MarkMc

Ruby-remove leading zero

Recommended Posts

Looking to make macro that I can use for certain labels- will need to read something like RBEP.75XXXX or RBEP1.5XXXX (I can get the X's:)

What I have now gives me "0.75" instead of .75. ; naturally the 1.5 is ok Is there a way to strip off the leading zero?

Secondly for similar in another brand-is there a way to also strip off the both the zero AND the decimal point?

 

Since I'm trying to learn this stuff ...a hint to the correct reference section in all the info out there would be better than a this is what you do. (Like you have to convert to a string? or whatever- teach a man to fish)

 

 

Been slogging along trying to learn this stuff and boy I certainly ain't the sharpest tool in the shed. Waiting for a physical book to arrive hoping that helps or one day lightening strikes.

Share this post


Link to post
Share on other sites

 Here's one method 

 

value.to_s.reverse.chomp("0.").reverse

 

I think there is probably a sprintf function that might do the trick too.  

 

There are probably a good half dozen other methods I could come up with too.

 

I think the reverse.chomp.reverse method is probably as good as any though.  

 

Essentially what you are doing is converting your value to a string, turning it around, specifying which characters you want removed from the end, and then turning it back around. 

  • Upvote 1

Share this post


Link to post
Share on other sites

Thanks, so that is "turn it into a string" sort of thing, then make the change there, yes?

Share this post


Link to post
Share on other sites

Mark,

string_value.gsub("0.",",") for the 1st one

string_value.gsub("0.","") for the 2nd one

 

The gsub method just substitutes characters within a string

  • Upvote 1

Share this post


Link to post
Share on other sites
2 minutes ago, MarkMc said:

Thanks, so that is "turn it into a string" sort of thing, then make the change there, yes?

  Sorry Mark, I was actually editing my previous post when you posted but in short...yes.   

Share this post


Link to post
Share on other sites

Yeah I've been there- just need a map to get around :0

No longer have a marked solved  on threads?

Thanks Guys

Share this post


Link to post
Share on other sites
Just now, MarkMc said:

Yeah I've been there- just need a map to get around :0

No longer have a marked solved  on threads?

Thanks Guys

So in this case you have a string value.  Scroll down to string, select it and then scroll down to see all the available methods. 

In the case of your strings I used gsub(str1,str2) which changes all matches.  You could just use sub(str1,str2) which just changes the first match.

Share this post


Link to post
Share on other sites

 No problem.   One quick note  regarding the .gsub (global substitution) method.   Be careful how and where are you use that as it will substitute every instance of that character pattern from your string.  Sometimes it's a better idea to simply use .sub  which only removes the first instance of that character pattern or substring.

 

EDIT:   I see Joe posted the same thing while I was posting. 

Share this post


Link to post
Share on other sites
8 minutes ago, MarkMc said:

No longer have a marked solved  on threads?

Points works B)

FWIW, I'm glad we no longer have the "Solved-Best Answer".  Too many users were marking their own post as the "Solved-Best Answer".  :(

Share this post


Link to post
Share on other sites

x.to_s.sub(/^(-)?0\./,'\1.')

where x= your number as in(-0.7500 Also handles negative numbers.

Also returns a string since all floats in Ruby are defined with a leading digit.

Share this post


Link to post
Share on other sites

 I'm sure Joe and Gerry realize this   but for anyone else reading along… 

 

 If you are trying to write a line of code that will work for any and all possible values (excluding negative numbers for the time being)  be careful using .sub or .gsub because for example where a=0.75 and b=20.5:

 

a.to_s.sub("0", "")  will return the desired .75

BUT

b.to_s.sub("0","")  will return 2.5

 

There are benefits and drawbacks to each of the various methods but in this particular example the reverse.chomp.reverse method has the benefit of only modifying the string if it starts with the specified character(s) (0 or 0. in this instance).  

 

If you want to more safely use .sub or .gsub  it would be probably a better idea to modify it to something like this...

 

if value.to_s.start_with? ("0")

 new_str = value.to_s.sub("0", "")

else

 new_str = value

end

 

 

 

P.S.  There's a good chance that Gerry's hieroglyphics  contained a shorthand version of that same sequence but I really can't be sure. To be honest I don't really understand most of that shorthand.  It's probably a lot more efficient but its harder to read IMO (even if I did understand it...which I don't). 

Share this post


Link to post
Share on other sites

it's called regex expressions and is common to all recent programming languages. Ruby just does a better job at implementing it. If your interested take a look at

 

"The bastards book of Ruby" available on the Web. Fairly straight forward stuff.

Share this post


Link to post
Share on other sites

def ltrim(a, char)

a.to_s.gsub(/^#{Regexp.escape(char)}+/, '')

end

 

ltrim(0.75, '0')

".75"

 

One liner

"0.75".gsub(/^#{0}+/, '')

 

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

  • Member Statistics

    27877
    Total Members
    6254
    Most Online
    Deller
    Newest Member
    Deller
    Joined