Round to two decimal place

Hi
   I have a variable say @price. It is either blank or has a value. If
it is blank I have to show it like $ 0.00 . So I wrote helper like

@price.blank? ? '$'+'0.00' : '$'+@price.to_s

     But the problem if @price has value 400.00 , after doing
@price.to_s it changes to 400.0 What I want is exactly 400.00 .And if
I remove to_s above it will throw an error
can't convert BigDecimal into String

    Please help
Thanks
Tom

···

--
Posted via http://www.ruby-forum.com/.

Hi
I have a variable say @price. It is either blank or has a value. If
it is blank I have to show it like $ 0.00 . So I wrote helper like

@price.blank? ? '$'+'0.00' : '$'+@price.to_s

But the problem if @price has value 400\.00  , after doing

@price.to_s it changes to 400.0 What I want is exactly 400.00 .And if
I remove to_s above it will throw an error
can't convert BigDecimal into String

try methods to_i, to_f, and %

eg

"$%6.2f" % " ".to_f

=> "$ 0.00"

"$%6.2f" % 400.to_f

=> "$400.00"

best regards -botp

···

On Tue, Sep 7, 2010 at 4:16 PM, Tom Mac <to.tom.mak@gmail.com> wrote:

Based on your use of the word "helper" I assume you're using Rails (this is
not a Rails forum), if that is so, try

Otherwise, check out sprintf

require 'bigdecimal'
@price = BigDecimal.new "400.00"
sprintf( "$%.02f" , @price ) # => "$400.00"

For more details on how to create a format string, the docs for sprintf are
pretty good http://ruby-doc.org/ruby-1.9/classes/Kernel.html#M002627

···

On Tue, Sep 7, 2010 at 3:16 AM, Tom Mac <to.tom.mak@gmail.com> wrote:

Hi
  I have a variable say @price. It is either blank or has a value. If
it is blank I have to show it like $ 0.00 . So I wrote helper like

@price.blank? ? '$'+'0.00' : '$'+@price.to_s

    But the problem if @price has value 400.00 , after doing
@price.to_s it changes to 400.0 What I want is exactly 400.00 .And if
I remove to_s above it will throw an error
can't convert BigDecimal into String

   Please help
Thanks
Tom
--
Posted via http://www.ruby-forum.com/\.

Tom, because you are dealing with prices, you might be interested in
this money gem
http://github.com/RubyMoney/money

Examples of usage in your context

Money.us_dollar(0).format

=> “$0.00”

Money.us_dollar(40.345).format

=> “$0.40”

#Eugen

> >   Hi
> > I have a variable say @price. It is either blank or has a value. If
> > it is blank I have to show it like $ 0.00 . So I wrote helper like
> > @price.blank? ? '$'+'0.00' : '$'+@price.to_s
> > But the problem if @price has value 400.00 , after doing
> > @price.to_s it changes to 400.0 What I want is exactly 400.00 .And if
> > I remove to_s above it will throw an error
> > can't convert BigDecimal into String
> > Please help
> > Thanks
> > Tom
> > --
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> Based on your use of the word "helper" I assume you're using Rails (this is
> not a Rails forum), if that is so, try
> [http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_currency](http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_currency)
> 
> 
> 
> Otherwise, check out sprintf
> require 'bigdecimal'
> @price = BigDecimal.new "400.00"
> sprintf( "$%.02f" , @price ) # => "$400.00"
> For more details on how to create a format string, the docs for sprintf are
> pretty good [http://ruby-doc.org/ruby-1.9/classes/Kernel.html#M002627](http://ruby-doc.org/ruby-1.9/classes/Kernel.html#M002627)
···

On 09/07/2010 12:41 PM, Josh Cheek wrote:

On Tue, Sep 7, 2010 at 3:16 AM, Tom Mac to.tom.mak@gmail.com wrote:

Tom, because you are dealing with prices, you might be interested in
this money gem

Examples of usage in your context

Money.us_dollar(0).format

=> "$0.00"

Money.us_dollar(40.345).format

=> "$0.40"

#Eugen

···

--
Posted via http://www.ruby-forum.com/\.