Round to two decimal place

Discussion in 'Ruby' started by Tom Mac, Sep 7, 2010.

  1. Tom Mac

    Tom Mac Guest

    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' : '$'_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 Mac, Sep 7, 2010
    #1
    1. Advertising

  2. Tom Mac

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Tue, Sep 7, 2010 at 3:16 AM, Tom Mac <> 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' : '$'_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/.
    >
    >

    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



    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
    Josh Cheek, Sep 7, 2010
    #2
    1. Advertising

  3. Tom Mac

    Eugen Ciur Guest

    Eugen Ciur, Sep 7, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ஆமாசà¯à®šà¯

    round off to two decimal & return float

    ஆமாசà¯à®šà¯, Mar 30, 2013, in forum: Python
    Replies:
    2
    Views:
    132
    pyplexed
    Mar 30, 2013
  2. ஆமாசà¯à®šà¯

    Re: round off to two decimal & return float

    ஆமாசà¯à®šà¯, Mar 30, 2013, in forum: Python
    Replies:
    1
    Views:
    84
    Grant Edwards
    Mar 30, 2013
  3. Peter Otten
    Replies:
    0
    Views:
    93
    Peter Otten
    Mar 30, 2013
  4. Dennis Lee Bieber

    Re: round off to two decimal & return float

    Dennis Lee Bieber, Mar 30, 2013, in forum: Python
    Replies:
    1
    Views:
    97
  5. ஆமாசà¯à®šà¯

    Re: round off to two decimal & return float

    ஆமாசà¯à®šà¯, Mar 30, 2013, in forum: Python
    Replies:
    0
    Views:
    57
    ஆமாசà¯à®šà¯
    Mar 30, 2013
Loading...

Share This Page