# Problem with display of float types

**URL:** <https://www.ruby-forum.com/t/problem-with-display-of-float-types/116297>\
**Category:** Rails\
**Created:** [October 4, 2007, 9:04am UTC](https://www.ruby-forum.com/t/problem-with-display-of-float-types/116297 "2007-10-04T09:04:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 4, 2007, 9:04am UTC](https://www.ruby-forum.com/t/problem-with-display-of-float-types/116297/1 "2007-10-04T09:04:46Z")

</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:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 4, 2007, 9:46am UTC](https://www.ruby-forum.com/t/problem-with-display-of-float-types/116297/2 "2007-10-04T09:46:17Z")

</div>

hi,  
if you only wanna display it precisely as you wish, use “printf”

Br

On 10ÔÂ4ÈÕ, ÉÏÎç10Ê±03·Ö, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 4, 2007, 9:58am UTC](https://www.ruby-forum.com/t/problem-with-display-of-float-types/116297/3 "2007-10-04T09:58:33Z")

</div>

Think theres a bit missing off the end?

Can I use printf inside a rhtml?

Charles

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 4, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/problem-with-display-of-float-types/116297/4 "2007-10-04T20:23:45Z")

</div>

On Oct 4, 6:52 am, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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:

```
  # Formats a +number+ with the specified level of +precision+

```

(e.g., 112.32 has a precision of 2). The default  
# level of precision is 3.  
#  
# ==== Examples  
# number\_with\_precision(111.2345) # =\> 111.235  
# number\_with\_precision(111.2345, 2) # =\> 111.24  
# number\_with\_precision(13, 5) # =\> 13.00000  
# number\_with\_precision(389.32314, 0) # =\> 389  
def number\_with\_precision(number, precision=3)  
“%01.#{precision}f” % number  
rescue  
number  
end

111.2345 to two decimal places should be 111.23, not 111.24.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 4, 2007, 10:19pm UTC](https://www.ruby-forum.com/t/problem-with-display-of-float-types/116297/5 "2007-10-04T22:19:45Z")

</div>

On Oct 4, 2007, at 2:22 PM, Stan K. wrote:

> ```
> "%01.#{precision}f" % number
> rescue
> number
> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 4, 2007, 3:53pm UTC](https://www.ruby-forum.com/t/problem-with-display-of-float-types/116297/6 "2007-10-04T15:53:44Z")

</div>

On Oct 4, 2007, at 3:57 AM, removed\_email\_address@domain.invalid wrote:

> 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

> > > Thanks,  
> > > Charles

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 5, 2007, 10:27am UTC](https://www.ruby-forum.com/t/problem-with-display-of-float-types/116297/7 "2007-10-05T10:27:18Z")

</div>

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