Add trailing zero in Ruby using format string technique

Hi,

If I want to pad with `0`, then the below is fine :

"$%d.%02d" % [12344444444444, 1] # => "$12344444444444.01"

If, I want to remove trailing `0`, then simply :

"%g" % 1.0 # 1

But, I am looking for how can I add trailing `0`s . I want "$12344444444444.10" instead of `"$12344444444444.01"`.

···

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan

I’m not sure if it’s the best way, but you could left justify the last digit and then do a gsub on spaces to put the 0’s in?

2.1.3 :011 > ("$%d.%-05d" % [12344444444444, 1]).gsub(/\s/, '0')

=> "$12344444444444.10000”

2.1.3 :012 > sprintf("$%d.%-05d", 12344444444444, 1).gsub(/\s/, '0')

=> "$12344444444444.10000”

···


Sent from Mailbox

On Wed, Mar 11, 2015 at 2:07 PM, Arup Rakshit <aruprakshit@rocketmail.com> wrote:

Hi,
If I want to pad with `0`, then the below is fine :
"$%d.%02d" % [12344444444444, 1] # => "$12344444444444.01"
If, I want to remove trailing `0`, then simply :
"%g" % 1.0 # 1
But, I am looking for how can I add trailing `0`s . I want "$12344444444444.10" instead of `"$12344444444444.01"`.
--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan

This is covered well in the documentation on sprintf (ri sprintf or man 2
printf).

"%.2f" % 13.5

=> "13.50"

"%d.%02d" % (13.5 * 100).divmod(100)

=> "13.50"

With your example, it should be:

"%d.%02d" % [12344444444444, 10]

-a

···

On Wed, Mar 11, 2015 at 5:23 PM, Raj Sahae <rajsahae@gmail.com> wrote:

I'm not sure if it's the best way, but you could left justify the last
digit and then do a gsub on spaces to put the 0's in?

2.1.3 :011 > ("$%d.%-05d" % [12344444444444, 1]).gsub(/\s/, '0')
=> "$12344444444444.10000"
2.1.3 :012 > sprintf("$%d.%-05d", 12344444444444, 1).gsub(/\s/, '0')
=> "$12344444444444.10000"

--
Sent from Mailbox <https://www.dropbox.com/mailbox&gt;

On Wed, Mar 11, 2015 at 2:07 PM, Arup Rakshit <aruprakshit@rocketmail.com> > wrote:

Hi,

If I want to pad with `0`, then the below is fine :

"$%d.%02d" % [12344444444444, 1] # => "$12344444444444.01"

If, I want to remove trailing `0`, then simply :

"%g" % 1.0 # 1

But, I am looking for how can I add trailing `0`s . I want
"$12344444444444.10" instead of `"$12344444444444.01"`.

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.

--Brian Kernighan

--
Austin Ziegler * halostatue@gmail.com * austin@halostatue.ca
http://www.halostatue.ca/ * http://twitter.com/halostatue

I never said I'm working on _float_ point numbers. I just said, you can do left _padd_ with `sprintf` or `String#%` I'm sure. But I am not sure, right padding is possible or not. Like :

Thu Mar 12 21:04:42 IST 2015
[arup@~]$ irb
[1] pry(main)> "%4s" % "a"
=> " a"
[2] pry(main)> "%4d" % "1"
=> " 1"
[3] pry(main)> "%40d" % "1"
=> " 1"
[4] pry(main)> "%04d" % "1"
=> "0001"
[5] pry(main)>

I also know right padding is possible using `String#ljust` like :

[5] pry(main)> "a".ljust(4, " ")
=> "a "
[6] pry(main)> "1".ljust(4, " ")
=> "1 "
[7] pry(main)> "1".ljust(4, '0')
=> "1000"
[8] pry(main)>

Just wanted to know is this really not possible using `String#%` or `#sprintf` ?

···

On Thursday, March 12, 2015 10:41:40 AM Austin Ziegler wrote:

This is covered well in the documentation on sprintf (ri sprintf or man 2
printf).

>> "%.2f" % 13.5
=> "13.50"
>> "%d.%02d" % (13.5 * 100).divmod(100)
=> "13.50"

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan

You cannot right-pad an integer with 0s using sprintf because that
changes the number. So you would no longer be formatting it, you'd be
outputting an entirely new number.

martin

···

On Thu, Mar 12, 2015 at 7:43 AM, Arup Rakshit <aruprakshit@rocketmail.com> wrote:

On Thursday, March 12, 2015 10:41:40 AM Austin Ziegler wrote:

This is covered well in the documentation on sprintf (ri sprintf or man 2
printf).

>> "%.2f" % 13.5
=> "13.50"
>> "%d.%02d" % (13.5 * 100).divmod(100)
=> "13.50"

I never said I'm working on _float_ point numbers. I just said, you can do left _padd_ with `sprintf` or `String#%` I'm sure. But I am not sure, right padding is possible or not. Like :

Thu Mar 12 21:04:42 IST 2015
[arup@~]$ irb
[1] pry(main)> "%4s" % "a"
=> " a"
[2] pry(main)> "%4d" % "1"
=> " 1"
[3] pry(main)> "%40d" % "1"
=> " 1"
[4] pry(main)> "%04d" % "1"
=> "0001"
[5] pry(main)>

I also know right padding is possible using `String#ljust` like :

[5] pry(main)> "a".ljust(4, " ")
=> "a "
[6] pry(main)> "1".ljust(4, " ")
=> "1 "
[7] pry(main)> "1".ljust(4, '0')
=> "1000"
[8] pry(main)>

Just wanted to know is this really not possible using `String#%` or `#sprintf` ?

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan

maybe I don't understand the question, but why not just multiply the 2nd
number by 10, and do a %02d on it?

···

On Thu, Mar 12, 2015 at 11:34 AM, Martin DeMello <martindemello@gmail.com> wrote:

You cannot right-pad an integer with 0s using sprintf because that
changes the number. So you would no longer be formatting it, you'd be
outputting an entirely new number.

martin

On Thu, Mar 12, 2015 at 7:43 AM, Arup Rakshit > <aruprakshit@rocketmail.com> wrote:
> On Thursday, March 12, 2015 10:41:40 AM Austin Ziegler wrote:
>> This is covered well in the documentation on sprintf (ri sprintf or man
2
>> printf).
>>
>> >> "%.2f" % 13.5
>> => "13.50"
>> >> "%d.%02d" % (13.5 * 100).divmod(100)
>> => "13.50"
>>
>
> I never said I'm working on _float_ point numbers. I just said, you can
do left _padd_ with `sprintf` or `String#%` I'm sure. But I am not sure,
right padding is possible or not. Like :
>
> Thu Mar 12 21:04:42 IST 2015
> [arup@~]$ irb
> [1] pry(main)> "%4s" % "a"
> => " a"
> [2] pry(main)> "%4d" % "1"
> => " 1"
> [3] pry(main)> "%40d" % "1"
> => " 1"
> [4] pry(main)> "%04d" % "1"
> => "0001"
> [5] pry(main)>
>
> I also know right padding is possible using `String#ljust` like :
>
> [5] pry(main)> "a".ljust(4, " ")
> => "a "
> [6] pry(main)> "1".ljust(4, " ")
> => "1 "
> [7] pry(main)> "1".ljust(4, '0')
> => "1000"
> [8] pry(main)>
>
>
> Just wanted to know is this really not possible using `String#%` or
`#sprintf` ?
>
> --
> ================
> Regards,
> Arup Rakshit
> ================
> Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
>
> --Brian Kernighan

--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer

*The most exciting phrase to hear in science - the one that heralds new
discoveries - is not "Eureka!" but "That's funny...".*- Isaac. Asimov

*I*
*f you give someone a program, you will frustrate them for a day; if you
teach them how to program, you will frustrate them for a lifetime. *-
Anonymous

*If writing good code requires very little comments, then writing really
excellent code requires no comments at all!*- Ken Thompson