# Always show 2 decimal places

**URL:** <https://www.ruby-forum.com/t/always-show-2-decimal-places/96642>\
**Category:** Rails\
**Created:** [June 30, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/always-show-2-decimal-places/96642 "2006-06-30T12:54:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 30, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/always-show-2-decimal-places/96642/1 "2006-06-30T12:54:28Z")

</div>

Hi,

I have a float that often looks like somthing like 2003.2 in my  
controller (1 decimal place)

Is there a way to always show it to 2 decimal places eg 2003.20 ?

Thanks  
Scott

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 30, 2006, 1:02pm UTC](https://www.ruby-forum.com/t/always-show-2-decimal-places/96642/2 "2006-06-30T13:02:19Z")

</div>

Ruby uses"printf" format conventions.

You can use:

printf (“%.2f”,2003.2) ==\> 2003.20

## \_Hari

View this message in context:  
[http://www.nabble.com/always-show-2-decimal-places-tf1872577.html#a5118477](http://www.nabble.com/always-show-2-decimal-places-tf1872577.html#a5118477)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 30, 2006, 1:35pm UTC](https://www.ruby-forum.com/t/always-show-2-decimal-places/96642/3 "2006-06-30T13:35:50Z")

</div>

Nara H. wrote:

> Ruby uses"printf" format conventions.
> 
> You can use:
> 
> printf ("%.2f",2003.2) ==\> 2003.20

I need to be able to store this in a nother varibale in a controller, is  
this possible?

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 30, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/always-show-2-decimal-places/96642/4 "2006-06-30T16:09:51Z")

</div>

Just FYI: If you see only one decimal place inside of a form field, you  
might want to look at the precision (number of decimal places) in your  
database. For example, MySQL defaults to 1 for floats. As soon as you  
change  
the precision to 2, you’ll see 2 places. This is, of course, only when  
using \<%=text\_field %\> and similar.

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 30, 2006, 2:28pm UTC](https://www.ruby-forum.com/t/always-show-2-decimal-places/96642/5 "2006-06-30T14:28:30Z")

</div>

sprintf("%.2f", number)

just an idea:

class Float  
def to\_s(digits)  
sprintf("%.#{digits}f", self)  
end  
end

12.0.to\_s(2)

> 12.00

ss schrieb:
