esamattis / esamattis/underscore.string

_.numberFormat not support the number is a float.

Open
#245 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
3.4k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

the _.numberFormat not support the number is a float.

example:

var tmpNumber = 10000.123;
_.numberFormat(tmpNumber )

it println: 10,000

i want it output 10,000.123

when we use this function, i don't care the number of variable "tmpNumber" is an int or float , also i don't want it output an int string. I only hope it format the int part of variable "tmpNumber", I want the output is the "10,000.123"

but now this function not support this. i modify the function used in my app.

on below :

```
numberFormat : function(number, dec, dsep, tsep) {
if (isNaN(number) || number == null) return '';

//begin: modify here
if(!isNaN(parseInt(dec,10))){
number = number.toFixed(~~dec);
}else{
number = number.toString();
}
//end

tsep = typeof tsep == 'string' ? tsep : ',';

var parts = number.split('.'), fnums = parts[0],
decimals = parts[1] ? (dsep || '.') + parts[1] : '';

return fnums.replace(/(\d)(?=(?:\d{3})+$)/g, '$1' + tsep) + decimals;
},
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.