i need to represent data as percentages

Discussion in 'Ruby' started by Trick Nick, Oct 23, 2008.

  1. Trick Nick

    Trick Nick Guest

    basically i go through a document count up the total number of times the
    status codes appeared. eg 200 = 96 201 =8 300 = 12 etc etc. I have
    counted up the total number of codes in the whole file and now must
    divide by each source codes value or individual count so it would be
    something like 108.to_f / 92.to_f but i get these numbers such as
    0.86899 yadayadayada. How do i convert that float number now to a
    representable percent such as 87%?
    --
    Posted via http://www.ruby-forum.com/.
     
    Trick Nick, Oct 23, 2008
    #1
    1. Advertising

  2. Trick Nick

    Tim Hunter Guest

    Trick Nick wrote:
    > basically i go through a document count up the total number of times the
    > status codes appeared. eg 200 = 96 201 =8 300 = 12 etc etc. I have
    > counted up the total number of codes in the whole file and now must
    > divide by each source codes value or individual count so it would be
    > something like 108.to_f / 92.to_f but i get these numbers such as
    > 0.86899 yadayadayada. How do i convert that float number now to a
    > representable percent such as 87%?




    irb(main):008:0> x = 0.86899
    => 0.86899
    irb(main):009:0> "%2d%%" % (x * 100.0)
    => "86%"
    irb(main):010:0>



    ~$ ri String#%
    --------------------------------------------------------------- String#%
    str % arg => new_str
    ------------------------------------------------------------------------
    Format---Uses str as a format specification, and returns the
    result of applying it to arg. If the format specification contains
    more than one substitution, then arg must be an Array containing
    the values to be substituted. See Kernel::sprintf for details of
    the format string.

    "%05d" % 123 #=> "00123"
    "%-5s: %08x" % [ "ID", self.id ] #=> "ID : 200e14d6"

    ~$

    --
    RMagick: http://rmagick.rubyforge.org/
     
    Tim Hunter, Oct 23, 2008
    #2
    1. Advertising

  3. Tim Hunter wrote:
    > irb(main):008:0> x = 0.86899
    > => 0.86899
    > irb(main):009:0> "%2d%%" % (x * 100.0)
    > => "86%"
    > irb(main):010:0>


    Or better, if you want round-to-nearest:

    irb(main):009:0> "%2.0f%%" % (x * 100)
    => "87%"
    irb(main):010:0> "%2.1f%%" % (x * 100)
    => "86.9%"
    irb(main):011:0> "%2.2f%%" % (x * 100)
    => "86.90%"
    irb(main):012:0> "%2.3f%%" % (x * 100)
    => "86.899%"
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Oct 23, 2008
    #3
  4. 2008/10/23 Brian Candler <>:
    > Tim Hunter wrote:
    >> irb(main):008:0> x = 0.86899
    >> => 0.86899
    >> irb(main):009:0> "%2d%%" % (x * 100.0)
    >> => "86%"
    >> irb(main):010:0>

    >
    > Or better, if you want round-to-nearest:
    >
    > irb(main):009:0> "%2.0f%%" % (x * 100)
    > => "87%"
    > irb(main):010:0> "%2.1f%%" % (x * 100)
    > => "86.9%"
    > irb(main):011:0> "%2.2f%%" % (x * 100)
    > => "86.90%"
    > irb(main):012:0> "%2.3f%%" % (x * 100)
    > => "86.899%"


    In that case you can do "%.1f%%" % (x * 100), i.e. you do not need
    the number before the dot. This number is for fixing the length of
    the whole expression:

    irb(main):001:0> "%.2f" % 1.2345
    => "1.23"
    irb(main):002:0> "%10.2f" % 1.2345
    => " 1.23"
    irb(main):003:0> "%1.2f" % 1.2345
    => "1.23"
    irb(main):004:0> "%0.2f" % 1.2345
    => "1.23"
    irb(main):005:0> "%5.2f" % 1.2345
    => " 1.23"

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
     
    Robert Klemme, Oct 23, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dot net work
    Replies:
    2
    Views:
    1,597
    Dot Net Work
    May 20, 2004
  2. Nathan Sokalski
    Replies:
    2
    Views:
    488
    =?Utf-8?B?R2FyeSBL?=
    Jun 15, 2005
  3. Marc  Dugger
    Replies:
    1
    Views:
    6,878
    Marc Dugger
    Dec 10, 2004
  4. Marc  Dugger
    Replies:
    0
    Views:
    538
    Marc Dugger
    Dec 10, 2004
  5. Nathan Sokalski

    Specifying Width and Height properties as percentages

    Nathan Sokalski, Jun 13, 2005, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    136
    Gary K
    Jun 15, 2005
Loading...

Share This Page