Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for floats. See related issue. #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jvonmitchell
Copy link

Now floats will pad.

We are using a new strategy here.  It involves math!  Yeah math!
It also uses lodash to more efficiently and legibly produce the string.
Lodash is needed for its string utilities.
Input n into the equation of number of digits in the integer component.
Use max instead of min for number of padding characters to add.
@jvonmitchell
Copy link
Author

This is a better pull request.

@Daiz
Copy link
Owner

Daiz commented Feb 8, 2015

Adding lodash as a dependency for a module as small and simple as this feels pretty silly, especially when it could be replaced with a simple for or while loop. It's more bloat than efficiency. And speaking of efficiency, the math approach is pretty illegible and slow - a simple regex approach using /(\d+)(\.\d+)/works much faster based on quick testing (see this jsperf).

With all that said, I'm not even sure if I'd want zpad to pad floats like this all the time - the basic idea of padding is to get strings of equal length, and decimals could easily wreck that. With that in mind, I'm considering adding an extra parameter float, where you could define padding for decimals. The idea is that you could then do this:

var zpad = require('zpad').amount(3).float(5); // => will give strings with length 9 minimum

zpad(35.582);     // =>  035.58200
zpad(2.48);       // =>  002.48000
zpad(0);          // =>  000.00000
zpad(847.2248);   // =>  847.22480
zpad(5982.493103);// => 5982.493103

zpad.float(null); // no special float treatment, default?
zpad(3.2); // => 3.2
zpad(.2);  // => 0.2

zpad.float(0); // ignores floats
zpad(4.2873); // => 004.2873

So basically, setting zpad.float(0) would give the behavior you desire here. I'm a bit undecided on what to have as the default behavior, though - keep the current one, or use .float(0)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants