Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Using Regular Expressions to Simulate sprintf() function - One Liner Challenge

by Incognito (Pilgrim)
on Nov 07, 2001 at 00:05 UTC ( [id://123672]=note: print w/replies, xml ) Need Help??


in reply to Re: Using Regular Expressions to Simulate sprintf() function - One Liner Challenge
in thread Using Regular Expressions to Simulate sprintf() function - One Liner Challenge

Yes, good point... We're not really simulating sprintf(), but just the part where we take a number and add a decimal place at one part of a number and format it to our liking...

I think a lot of people didn't notice that the Perl code was just a snippet of what was in the actual function, and that I didn't provide the entire thing... if we examine the JavaScript version, we will notice that it handles the special cases:

strValue += ""; // Convert any digits to string if (! strValue) { strValue = "0"; } // Convert empty string to 0 // Set any non-numeric values to 0 if (! strValue.match (/^\d+\.?$|\d+\.?\d*$|\d*\.?\d+$/)) { strValue += "0"; } // Add a leading 0 to any numbers starting with a decimal point if (strValue.match (/^\./)) { strValue = "0" + strValue; }

So what we really have (without actually provided the entire function :) is:

value|precision sprintf Incognito Albannach jeffa r +ob_au '123.45678'|3: 123.457 123.456 123.456 123 12 +3.457 '12.4'|1: 12.4 12.4 12.4 12 + 12.4 ''|0: 0 0 + 0 '3.14'|5: 3.14000 3.14000 3.14000 00003.14 3. +14000 '1'|5: 1.00000 1.00000 1.00000 00001 1. +00000 '.5'|0: 0 0 + 1 '10'|5: 10.00000 10.00000 10.00000 00010 10. +00000 '12.45435'|0: 12 12 12 12 + 12 '100'|5: 100.00000 100.00000 100.00000 00100 100. +00000 '3.'|0: 3 3 3 3 + 3 '1000'|5: 1000.00000 1000.00000 1000.00000 01000 1000. +00000 '12.56'|0: 13 12 12 12 + 13
  • Comment on Re: Re: Using Regular Expressions to Simulate sprintf() function - One Liner Challenge
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://123672]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-25 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found