# I need to represent data as percentages

**URL:** <https://www.ruby-forum.com/t/i-need-to-represent-data-as-percentages/151569>\
**Category:** Ruby\
**Created:** [October 23, 2008, 2:55am UTC](https://www.ruby-forum.com/t/i-need-to-represent-data-as-percentages/151569 "2008-10-23T02:55:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![silentd3at4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@silentd3at4](https://www.ruby-forum.com/u/silentd3at4)\
**Post date:** [October 23, 2008, 2:55am UTC](https://www.ruby-forum.com/t/i-need-to-represent-data-as-percentages/151569/1 "2008-10-23T02:55:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![silentd3at4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@silentd3at4](https://www.ruby-forum.com/u/silentd3at4)\
**Post date:** [October 23, 2008, 3:06am UTC](https://www.ruby-forum.com/t/i-need-to-represent-data-as-percentages/151569/2 "2008-10-23T03:06:38Z")

</div>

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"

```

~$

---

<div class="post-metadata">

**Author:** ![silentd3at4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@silentd3at4](https://www.ruby-forum.com/u/silentd3at4)\
**Post date:** [October 23, 2008, 10:52am UTC](https://www.ruby-forum.com/t/i-need-to-represent-data-as-percentages/151569/3 "2008-10-23T10:52:05Z")

</div>

Tim H. 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%”

---

<div class="post-metadata">

**Author:** ![silentd3at4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@silentd3at4](https://www.ruby-forum.com/u/silentd3at4)\
**Post date:** [October 23, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/i-need-to-represent-data-as-percentages/151569/4 "2008-10-23T14:04:44Z")

</div>

2008/10/23 Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> =\> “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
