# Number format

**URL:** <https://www.ruby-forum.com/t/number-format/78212>\
**Category:** Ruby\
**Created:** [December 4, 2006, 2:48am UTC](https://www.ruby-forum.com/t/number-format/78212 "2006-12-04T02:48:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 4, 2006, 2:48am UTC](https://www.ruby-forum.com/t/number-format/78212/1 "2006-12-04T02:48:38Z")

</div>

Hi all,

I have a number, for example, 1.123456789. What is the Ruby way to  
change it into whatever number of floating points such as 1.12,  
1.123,1.1234568 or 1.12345679.

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 4, 2006, 3:28am UTC](https://www.ruby-forum.com/t/number-format/78212/2 "2006-12-04T03:28:07Z")

</div>

You can use the number.round method, but this doesn’t take a number of  
decimal places.

To round 1.234 to 1.23 use this code:  
x = 1.234  
x = x \* 100 # -\> 123.4  
x = x.round -\> 123  
x = x / 100.0 =\> 1.23

At least, that’s the best method I can find from the Ruby docs…

Dan

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 4, 2006, 3:38am UTC](https://www.ruby-forum.com/t/number-format/78212/3 "2006-12-04T03:38:10Z")

</div>

On 12/4/06, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

It might be better to leave the precision alone until you print it, in  
which case, you can print it with #sprintf or String#% or similar:

irb(main):001:0\> ‘%.2f’ % 1.23456789  
=\> “1.23”  
irb(main):002:0\> ‘%.5f’ % 1.23456789  
=\> “1.23457”

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 4, 2006, 4:02am UTC](https://www.ruby-forum.com/t/number-format/78212/4 "2006-12-04T04:02:13Z")

</div>

On Mon, Dec 04, 2006 at 10:48:41AM +0900, Li Chen wrote:

> Hi all,
> 
> I have a number, for example, 1.123456789. What is the Ruby way to  
> change it into whatever number of floating points such as 1.12,  
> 1.123,1.1234568 or 1.12345679.

You could use facets:

```
% sudo gem install facets
% irb

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'facet/float/round_at'
=> true
irb(main):003:0> require 'facet/float/round_to'
=> true
irb(main):004:0> x = 1.123456789
=> 1.123456789
irb(main):005:0> x.round_at(3)
=> 1.123
irb(main):006:0> x.round_at(7)
=> 1.1234568
irb(main):007:0> x.round_at(100)
=> 1.123456789
irb(main):008:0> x.round_to(0.001)
=> 1.123
irb(main):009:0> x.round_to(0.000001)
=> 1.123457

```

enjoy,

-jeremy

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 4, 2006, 9:00am UTC](https://www.ruby-forum.com/t/number-format/78212/5 "2006-12-04T09:00:35Z")

</div>

Daniel F. wrote:

> You can use the number.round method, but this doesn’t take a number of  
> decimal places.
> 
> To round 1.234 to 1.23 use this code:  
> x = 1.234  
> x = x \* 100 # -\> 123.4  
> x = x.round -\> 123  
> x = x / 100.0 =\> 1.23
> 
> At least, that’s the best method I can find from the Ruby docs…

This is not a good idea. Because the number being manipulated is binary  
but  
the display is decimal, the multiplication and division steps will fail  
on  
some numbers – many, in fact – and those numbers will print more  
decimal  
places than was intended.

It’s better to retain the full binary resolution of a number internally,  
and  
only show certain decimal places while printing a number – don’t try to  
truncate the number itself.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 4, 2006, 3:58am UTC](https://www.ruby-forum.com/t/number-format/78212/6 "2006-12-04T03:58:12Z")

</div>

> It might be better to leave the precision alone  
> until you print it, in  
> which case, you can print it with #sprintf or  
> String#% or similar:
> 
> irb(main):001:0\> ‘%.2f’ % 1.23456789  
> =\> “1.23”  
> irb(main):002:0\> ‘%.5f’ % 1.23456789  
> =\> “1.23457”

I think I prefer the suggestion.

Li
