format a float number with specific number of decimal point

Discussion in 'Ruby' started by Oscar Lok, Nov 19, 2006.

  1. Oscar Lok

    Oscar Lok Guest

    Hi,
    May I know that how to format a float number with specific number of
    decimal point, please?
    e.g :--
    123.456789 to 123.4568
    123.123321 to 123.1233

    Thanks
    Regards
    Oscar

    --
    Posted via http://www.ruby-forum.com/.
     
    Oscar Lok, Nov 19, 2006
    #1
    1. Advertising

  2. Oscar Lok <> writes:

    > Hi,
    > May I know that how to format a float number with specific number of
    > decimal point, please?
    > e.g :--
    > 123.456789 to 123.4568
    > 123.123321 to 123.1233


    irb(main):039:0> print [123.456789, 123.123321].collect {|x| format("#{x} to %.4f\n",x)}
    123.456789 to 123.4568
    123.123321 to 123.1233
    => nil
    irb(main):040:0>

    --
    David Kastrup, Kriemhildstr. 15, 44793 Bochum
     
    David Kastrup, Nov 19, 2006
    #2
    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. Andy
    Replies:
    7
    Views:
    6,268
    Roedy Green
    May 10, 2004
  2. bd
    Replies:
    0
    Views:
    634
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,558
    James Kanze
    Oct 8, 2009
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,349
    Saraswati lakki
    Jan 6, 2012
  5. Fei Liu
    Replies:
    21
    Views:
    2,378
    John Bokma
    Dec 16, 2006
Loading...

Share This Page