From: Robert Klemme Date: 2008-10-23T21:02:31+09:00 Subject: Re: i need to represent data as percentages 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