# How to format decimals on the fly (for EOF blocks)?

**URL:** <https://www.ruby-forum.com/t/how-to-format-decimals-on-the-fly-for-eof-blocks/147098>\
**Category:** Ruby\
**Created:** [August 28, 2008, 1:27pm UTC](https://www.ruby-forum.com/t/how-to-format-decimals-on-the-fly-for-eof-blocks/147098 "2008-08-28T13:27:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![caiwangqin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@caiwangqin](https://www.ruby-forum.com/u/caiwangqin)\
**Post date:** [August 28, 2008, 1:27pm UTC](https://www.ruby-forum.com/t/how-to-format-decimals-on-the-fly-for-eof-blocks/147098/1 "2008-08-28T13:27:40Z")

</div>

Hello, I’m facing a problem where, for instance, 7mcg of Nutrient is  
required per day. It’s scaled in the db to 3 (or more) decimal places,  
but on the page, often there will show

```
Nutrient 0 mcg 8%

```

– otherwise … 0.4423 mcg 8%

My figures are for demonstration only 🙂

Is there a way to covert the latter mcg figure to 0.4 on the fly? Or do  
I need to work on the database, since these are mainly for string \<\<EOF  
output?

---

<div class="post-metadata">

**Author:** ![caiwangqin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@caiwangqin](https://www.ruby-forum.com/u/caiwangqin)\
**Post date:** [August 28, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/how-to-format-decimals-on-the-fly-for-eof-blocks/147098/2 "2008-08-28T14:04:43Z")

</div>

sprintf !

Jesse C. wrote:

> Hello, I’m facing a problem where, for instance, 7mcg of Nutrient is  
> required per day. It’s scaled in the db to 3 (or more) decimal places,  
> but on the page, often there will show
> 
> ```
> Nutrient 0 mcg 8%
> 
> ```
> 
> – otherwise … 0.4423 mcg 8%
> 
> My figures are for demonstration only 🙂
> 
> Is there a way to covert the latter mcg figure to 0.4 on the fly? Or do  
> I need to work on the database, since these are mainly for string \<\<EOF  
> output?
