i need to represent data as percentages

T

Trick Nick

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%?
 
T

Tim Hunter

Trick said:
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"

~$
 
B

Brian Candler

Tim said:
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%"
 
R

Robert Klemme

2008/10/23 Brian Candler said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top