Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check number and remove leading zeros

Tags:

I want to check a variable in ruby to see if it has two leading zeros (00) If it does the 00 should be removed

How can this be done

like image 489
veccy Avatar asked Aug 14 '10 11:08

veccy


People also ask

How do I remove leading zeros in VBA?

Using the Paste Special feature, we can add 0 to the column to remove leading zeros.


1 Answers

It's pretty easy to convert to an integer and convert back to a string:

irb(main):007:0> s="009" ; s.to_i.to_s => "9" irb(main):008:0> s="004" ; s.to_i.to_s => "4" irb(main):009:0> s="00999" ; s.to_i.to_s => "999" 

or, for floats:

irb(main):003:0> s="000.45" ; s.to_f.to_s => "0.45" 
like image 88
sarnold Avatar answered Sep 28 '22 18:09

sarnold