# How do I format return this value?

**URL:** <https://www.ruby-forum.com/t/how-do-i-format-return-this-value/191900>\
**Category:** Ruby\
**Created:** [July 20, 2010, 2:13pm UTC](https://www.ruby-forum.com/t/how-do-i-format-return-this-value/191900 "2010-07-20T14:13:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 20, 2010, 2:13pm UTC](https://www.ruby-forum.com/t/how-do-i-format-return-this-value/191900/1 "2010-07-20T14:13:37Z")

</div>

I have a method which calculates values and creates and object in a  
class

Acalculation.new(valuea, valueb)

they go to

def to\_s  
“#{@valuea}#{@valueb}”  
end

If valuea is 100 and valueb is 555555.5555

What would be the neatest and cleanest way to convert them to a  
formatted value like this (note the rounding also)

valuea - 100.00 valueb - 555,555.56

I’ve found lots of code that does bit and pieces like I need but not  
anything that does commas and rounding to 2 decimal places.

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 20, 2010, 2:48pm UTC](https://www.ruby-forum.com/t/how-do-i-format-return-this-value/191900/2 "2010-07-20T14:48:46Z")

</div>

On 20 juil. 2010, at 14:13, Chad W. wrote:

> valuea - 100.00 valueb - 555,555.56

“valuea - %.2f valueb - %.2f” % [valuea, valueb]

This returns

“valuea - 100.00 valueb - 555555.56”

Extra steps need to be taken for the thousands separator, I don’t think  
Ruby has anything built-in for that.

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 20, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/how-do-i-format-return-this-value/191900/3 "2010-07-20T16:31:52Z")

</div>

On Tue, Jul 20, 2010 at 1:13 PM, Chad W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> …

What would be the neatest and cleanest way to convert them to a

> formatted value like this (note the rounding also)  
> valuea - 100.00 valueb - 555,555.56  
> I’ve found lots of code that does bit and pieces like I need but not  
> anything that does commas and rounding to 2 decimal places.

The following methods (for Integers and Floats) are cut down versions of  
some more general number formatting methods I wrote for myself some time  
ago. No guarantees that they do what you want, but some quick tests seem  
to  
show they do.

class Integer

# Puts digit separators for each group of 3 digits,

# with option to use a separator which is not an underscore.

def to\_s!( grp\_sep = ‘\_’ )  
s = self.to\_s  
if grp\_sep then  
n = s.length  
ne = ( if self \< 0 then 1 else 0 end )  
while (n -= 3) \> ne do; s[n, 0] = grp\_sep; end  
end  
s  
end  
end

class Float

# Puts integer part digit separators for each group of 3 digits,

# with option to use a separator which is not an underscore.

# (if “.” is used as the digits separator then the "decimal

separator"

# will be changed from “.” to “,”);

def to\_s!( n\_dec\_places, grp\_sep = ‘\_’ )  
unless n\_dec\_places.kind\_of?( Integer ) then  
raise “Float#to\_s! - number of decimal places must be an integer”  
end  
unless n\_dec\_places \>= 0 then  
raise “Float#to\_s! - negative number of decimal places yet to be  
implemented to give robust results”  
end  
fmt\_str = “%.#{n\_dec\_places}f”  
s = sprintf( fmt\_str, self )  
if grp\_sep then  
n = s.rindex( ‘.’ ) # if n\_dec\_places == 0 then n = nil  
s[n, 1] = ‘,’ if n && grp\_sep == ‘.’  
n ||= s.length  
ne = ( if self \< 0 then 1 else 0 end )  
while (n -= 3) \> ne do; s[n, 0] = grp\_sep; end  
end  
s  
end  
end
