# Pls help me regarding Maths round up function

**URL:** <https://www.ruby-forum.com/t/pls-help-me-regarding-maths-round-up-function/62584>\
**Category:** Rails\
**Created:** [June 6, 2006, 10:04am UTC](https://www.ruby-forum.com/t/pls-help-me-regarding-maths-round-up-function/62584 "2006-06-06T10:04:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 6, 2006, 10:04am UTC](https://www.ruby-forum.com/t/pls-help-me-regarding-maths-round-up-function/62584/1 "2006-06-06T10:04:04Z")

</div>

Hi,

I have some values on my webpage displaying like  
1.22333333333  
2.33333344444  
2.33377777777  
etc.  
Here I want to display values upto 2 decimal places correct.  
i.e, 1.22333333333 should be dislayed as 1.22  
2.33333344444 should be dislayed as 2.33  
2.33777777777 should be dislayed as 2.34  
& so on…

How to do this in ruby???

Is there any function???

Thanx in advance.  
Prash

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 6, 2006, 10:14am UTC](https://www.ruby-forum.com/t/pls-help-me-regarding-maths-round-up-function/62584/2 "2006-06-06T10:14:05Z")

</div>

Hi.

(value\*100).round/100.0

Stan

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 6, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/pls-help-me-regarding-maths-round-up-function/62584/3 "2006-06-06T16:54:45Z")

</div>

On 6/6/06, Prashant T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 2.33777777777 should be dislayed as 2.34
> 
> ```
> 
> & so on…
> 
> How to do this in ruby???
> 
> Is there any function???

puts “%.2f” % 223.3234235235 # =\>223.32

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 7, 2006, 1:58pm UTC](https://www.ruby-forum.com/t/pls-help-me-regarding-maths-round-up-function/62584/4 "2006-06-07T13:58:39Z")

</div>

> > Is there any function???
> 
> puts “%.2f” % 223.3234235235 # =\>223.32

Hey Prash, this will round to a specific number of decimal pts:

```
def round_float(float_num,dec)
	(float_num*(10**dec)).round.to_f / (10**dec).to_f
end

round_float(1.238, 2) => 1.24

```

cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 6, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/pls-help-me-regarding-maths-round-up-function/62584/5 "2006-06-06T22:03:05Z")

</div>

Prashant T. wrote:

> Hi,
> 
> I have some values on my webpage displaying like  
> 1.22333333333  
> 2.33333344444  
> 2.33377777777  
> etc.  
> Here I want to display values upto 2 decimal places correct.  
> i.e, 1.22333333333 should be dislayed as 1.22  
> 2.33333344444 should be dislayed as 2.33  
> 2.33777777777 should be dislayed as 2.34  
> & so on…
> 
> How to do this in ruby???
> 
> Is there any function???
> 
> Thanx in advance.  
> Prash

The most elegant way is facets/round\_at

Check out [http://facets.rubyforge.org/doc/api/](http://facets.rubyforge.org/doc/api/)  
Specifically  
[http://facets.rubyforge.org/doc/api/classes/Float.html#M000201](http://facets.rubyforge.org/doc/api/classes/Float.html#M000201)

require facet/round\_at  
4.55555.round\_at(2) #=\> 4.56
