# Problem with display of float types

**URL:** <https://discuss.rubyonrails.org/t/problem-with-display-of-float-types/18865>\
**Category:** rubyonrails-talk\
**Created:** [October 4, 2007, 7:03am UTC](https://discuss.rubyonrails.org/t/problem-with-display-of-float-types/18865 "2007-10-04T07:03:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![charles.poulsen](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@charles.poulsen](https://discuss.rubyonrails.org/u/charles.poulsen)\
**Post date:** [October 4, 2007, 7:03am UTC](https://discuss.rubyonrails.org/t/problem-with-display-of-float-types/18865/1 "2007-10-04T07:03:56Z")

</div>

Hello all,

I am taking some values from mysql columns of type float (16, 2) , performing a calculation such as multiply or divide, before displaying on screen, and then displaying on a screen.

Unfortunately, sometimes the value has many decimal places displayed! (Eg 136.5325325) Is there a way I can limit it to only display 2 decimal places?

Thanks, Charles

---

<div class="post-metadata">

**Author:** ![myst\_tt](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@myst\_tt](https://discuss.rubyonrails.org/u/myst_tt)\
**Post date:** [October 4, 2007, 7:45am UTC](https://discuss.rubyonrails.org/t/problem-with-display-of-float-types/18865/2 "2007-10-04T07:45:33Z")

</div>

hi, &nbsp;&nbsp;&nbsp;&nbsp;if you only wanna display it precisely as you wish, use "printf"

Br

---

<div class="post-metadata">

**Author:** ![charles.poulsen](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@charles.poulsen](https://discuss.rubyonrails.org/u/charles.poulsen)\
**Post date:** [October 4, 2007, 7:57am UTC](https://discuss.rubyonrails.org/t/problem-with-display-of-float-types/18865/3 "2007-10-04T07:57:31Z")

</div>

Think theres a bit missing off the end?

Can I use printf inside a rhtml?

Charles

---

<div class="post-metadata">

**Author:** ![rab](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/rab/32/710_2.png) [@rab](https://discuss.rubyonrails.org/u/rab)\
**Post date:** [October 4, 2007, 1:52pm UTC](https://discuss.rubyonrails.org/t/problem-with-display-of-float-types/18865/4 "2007-10-04T13:52:59Z")

</div>

> Think theres a bit missing off the end?
> 
> Can I use printf inside a rhtml?
> 
> Charles

Of course you can, it's just Ruby. If you want to stay with a Rails solution, try number\_with\_precision(136.5325325, 2) (and then look at the source for that method and you won't need the method anymore)

-Rob

---

<div class="post-metadata">

**Author:** ![skaufman](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@skaufman](https://discuss.rubyonrails.org/u/skaufman)\
**Post date:** [October 4, 2007, 6:22pm UTC](https://discuss.rubyonrails.org/t/problem-with-display-of-float-types/18865/5 "2007-10-04T18:22:44Z")

</div>

number\_with\_precision doesn't round correctly, though. From the source:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Formats a +number+ with the specified level of +precision+ (e.g., 112.32 has a precision of 2). The default &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# level of precision is 3.

---

<div class="post-metadata">

**Author:** ![rab](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/rab/32/710_2.png) [@rab](https://discuss.rubyonrails.org/u/rab)\
**Post date:** [October 4, 2007, 8:18pm UTC](https://discuss.rubyonrails.org/t/problem-with-display-of-float-types/18865/6 "2007-10-04T20:18:50Z")

</div>

> > Of course you can, it's just Ruby. If you want to stay with a Rails solution, try number\_with\_precision(136.5325325, 2) (and then look at the source for that method and you won't need the method anymore)
> 
> number\_with\_precision doesn't round correctly, though. From the source:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Formats a +number+ with the specified level of +precision+ (e.g., 112.32 has a precision of 2). The default &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# level of precision is 3. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ==== Examples &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# number\_with\_precision(111.2345) # =\> 111.235 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# number\_with\_precision(111.2345, 2) # =\> 111.24 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# number\_with\_precision(13, 5) # =\> 13.00000 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# number\_with\_precision(389.32314, 0) # =\> 389 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def number\_with\_precision(number, precision=3) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"%01.#{precision}f" % number &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> 111.2345 to two decimal places should be 111.23, not 111.24.

The doc is wrong -- probably written by a human 😉

$ script/console Loading development environment. \>\> helper.number\_with\_precision(111.2345, 2) =\> "111.23"

I suspect the aforementioned human applied the precision=2 to the result of the precision=3 shown just above it rather than the original 111.2345 argument.

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com) Rob@AgileConsultingLLC.com

---

<div class="post-metadata">

**Author:** ![charles.poulsen](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@charles.poulsen](https://discuss.rubyonrails.org/u/charles.poulsen)\
**Post date:** [October 5, 2007, 8:26am UTC](https://discuss.rubyonrails.org/t/problem-with-display-of-float-types/18865/7 "2007-10-05T08:26:27Z")

</div>

Thanks guys, number\_with\_precision does the job for me.
