Quick question

Alright, I feel like an absolute idiot, but the question is simple, and 30
minutes of looking hasn't yet yeilded the answer.

I want to take a float (say 0.0) and output it with a space in front if it's
positive, - in front if it's negative, and be able to specify how many
numbers after the decimal get printed (padding with zeros). This will be
used to output currency, and I just know there's a simple way to do it with
Ruby, but not finding it. :slight_smile:

Sorry to bother you all.

printf "% .2f", 3.14159
3.14
printf "% .2f", -2
-2.00

best regards,
Ed

路路路

On Tue, Sep 13, 2005 at 12:25:28PM +0900, Kevin Brown wrote:

I want to take a float (say 0.0) and output it with a space in front if it's
positive, - in front if it's negative, and be able to specify how many
numbers after the decimal get printed (padding with zeros).

=> [" 123.46", "-123.46"]

You can also use printf with the same format for direct output.

Kind regards

    robert

路路路

Kevin Brown <blargity@gmail.com> wrote:

Alright, I feel like an absolute idiot, but the question is simple,
and 30 minutes of looking hasn't yet yeilded the answer.

I want to take a float (say 0.0) and output it with a space in front
if it's positive, - in front if it's negative, and be able to specify
how many numbers after the decimal get printed (padding with zeros). This will be used to output currency, and I just know there's a
simple way to do it with Ruby, but not finding it. :slight_smile:

Sorry to bother you all.

[123.456,-123.456].map{|f| sprintf "% 3.2f", f}

Or you can use the % operator on a string which I like better. I.e.
"%3.2f" % 1.0 #=> " 1.00"

regards,

Brian

路路路

On 13/09/05, Robert Klemme <bob.news@gmx.net> wrote:

Kevin Brown <blargity@gmail.com> wrote:
> Alright, I feel like an absolute idiot, but the question is simple,
> and 30 minutes of looking hasn't yet yeilded the answer.
>
> I want to take a float (say 0.0) and output it with a space in front
> if it's positive, - in front if it's negative, and be able to specify
> how many numbers after the decimal get printed (padding with zeros).
> This will be used to output currency, and I just know there's a
> simple way to do it with Ruby, but not finding it. :slight_smile:
>
> Sorry to bother you all.

>> [123.456,-123.456].map{|f| sprintf "% 3.2f", f}
=> [" 123.46", "-123.46"]

You can also use printf with the same format for direct output.

Kind regards

    robert

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

Just remember that if you have multiple formatted pieces in the same string, you need to supply the argument to the #% method as an array:

m = 57.23562
s = 83.333333
"%3.2f - %3.2f = %3.2f" % [ m, s, m-s ]

#=> "57.24 - 83.33 = -26.10"

The fun comes when you use string interpolation along with sprintf's codes:

class Numeric
     def rounded_to( decimals )
         decimals = decimals.round
         if decimals < 0
             factor = 10.0 ** -decimals
             ( ( self / factor ).round * factor ).round.to_s
         else
             "%.#{decimals}f" % self
         end
     end
end

[ 1, -1, 0.017, -0.017, 1254, -81.2412234 ].each{ |number|
     [5,2,1,0,-1,-2,-3].each{ |decimal_places|
         puts number.rounded_to( decimal_places )
     }
     puts '-' * 40
}

1.00000
1.00
1.0
1
0

路路路

On Sep 13, 2005, at 7:22 AM, Brian Schr枚der wrote:

Or you can use the % operator on a string which I like better. I.e.
"%3.2f" % 1.0 #=> " 1.00"

----------------------------------------
-1.00000
-1.00
-1.0
-1
0
----------------------------------------
0.01700
0.02
0.0
0
----------------------------------------
-0.01700
-0.02
-0.0
-0
0
----------------------------------------
1254.00000
1254.00
1254.0
1254
1250
1300
1000
----------------------------------------
-81.24122
-81.24
-81.2
-81
-80
-100
0
----------------------------------------

How would I format a number to simply be left-zero-padded with 2 floating
points?

eg.
000.24 from .238888888
001.24 from 1.237777777
010.25 from 10.249999999
100.25 from 100.24999999

I can do: %.2f to get the 2 decimals, but it doesn't do left-zero-pad.
I can do: %03d to pad 3 digits to the left, but it doesn't trim the decimals

I can do: printf("%03d%.2f", val, (val.to_f-val.to_i)) but that always
injects
an extra 0 like so:

0000.24
0010.24
0100.25
1000.25

I have yet to find a good (useful) doc on how the format args work, so point
me there but beware that most of the docs out there are super useless :slight_smile:

-henry

路路路

On 9/13/05, Gavin Kistner <gavin@refinery.com> wrote:

On Sep 13, 2005, at 7:22 AM, Brian Schr枚der wrote:
> Or you can use the % operator on a string which I like better. I.e.
> "%3.2f" % 1.0 #=> " 1.00"

Just remember that if you have multiple formatted pieces in the same
string, you need to supply the argument to the #% method as an array:

m = 57.23562
s = 83.333333
"%3.2f - %3.2f = %3.2f" % [ m, s, m-s ]

#=> "57.24 - 83.33 = -26.10"

The fun comes when you use string interpolation along with sprintf's
codes:

class Numeric
def rounded_to( decimals )
decimals = decimals.round
if decimals < 0
factor = 10.0 ** -decimals
( ( self / factor ).round * factor ).round.to_s
else
"%.#{decimals}f" % self
end
end
end

[ 1, -1, 0.017, -0.017, 1254, -81.2412234 ].each{ |number|
[5,2,1,0,-1,-2,-3].each{ |decimal_places|
puts number.rounded_to( decimal_places )
}
puts '-' * 40
}

1.00000
1.00
1.0
1
0
0
0
----------------------------------------
-1.00000
-1.00
-1.0
-1
0
0
0
----------------------------------------
0.01700
0.02
0.0
0
0
0
0
----------------------------------------
-0.01700
-0.02
-0.0
-0
0
0
0
----------------------------------------
1254.00000
1254.00
1254.0
1254
1250
1300
1000
----------------------------------------
-81.24122
-81.24
-81.2
-81
-80
-100
0
----------------------------------------

--
john hager
-------------------------------------
netcam@gmail.com
url: http://www.orgo.com
cell: 619.261.5990

How would I format a number to simply be left-zero-padded with 2 floating
points?

eg.
000.24 from .238888888
001.24 from 1.237777777
010.25 from 10.249999999
100.25 from 100.24999999

I can do: %.2f to get the 2 decimals, but it doesn't do left-zero-pad.
I can do: %03d to pad 3 digits to the left, but it doesn't trim the decimals

"%06.2f"

I can do: printf("%03d%.2f", val, (val.to_f-val.to_i)) but that always
injects
an extra 0 like so:

0000.24
0010.24
0100.25
1000.25

"%07.2f"

I have yet to find a good (useful) doc on how the format args work, so point
me there but beware that most of the docs out there are super useless :slight_smile:

printf(3) completely explains format strings.

路路路

On Nov 15, 2005, at 4:14 PM, hmmm wrote:

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

hmmm <netcam@gmail.com> writes:

How would I format a number to simply be left-zero-padded with 2 floating
points?

eg.
000.24 from .238888888
001.24 from 1.237777777
010.25 from 10.249999999
100.25 from 100.24999999

I can do: %.2f to get the 2 decimals, but it doesn't do left-zero-pad.
I can do: %03d to pad 3 digits to the left, but it doesn't trim the decimals

I can do: printf("%03d%.2f", val, (val.to_f-val.to_i)) but that always
injects
an extra 0 like so:

0000.24
0010.24
0100.25
1000.25

I have yet to find a good (useful) doc on how the format args work, so point
me there but beware that most of the docs out there are super useless :slight_smile:

Try this: printf("%07.2f", val)

The "7" is the total field width, including the decimal point. The "2"
represents the number of decimal places. The leading "0" means to
zero-pad.

路路路

--
Lloyd Zusman
ljz@asfast.com
God bless you.