Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
parse-decimal-number
Advanced tools
Readme
Parse a decimal number with i18n format support (localized decimal points and thousands separators)
OK, let’s fix international numbers parsing and validation once and forever. I got the inspiration for this in a UI project because somehow the libraries we used didn’t do a great job, so I wrote my own parser, and this is a more polished version of it.
These are the design goals:
NaN
for non-numbers. (=good for input validation) ✓cldr
data ✓In it’s simplest form, you just use it as a parseFloat
replacement.
npm i parse-decimal-number --save
parseDecimalNumber = require('parse-decimal-number');
console.log(parseDecimalNumber('12,345,678.90'));
// -> 12345678.90
console.log(parseDecimalNumber('12.345.678,90','.,'));
// -> 12345678.90
Returns a float
representation of string or NaN
if string is not a parseable number. Use the optional options
parameter to specify the thousands and decimal point characters.
string A String that is supposed to contain a number.
options optional A string, array or hash with thousands and decimal separators.
String
a two-character string consisting of the thousands character followed by the decimal point character, e.g. ',.'
Array
An array of two elements, the first being the thousands character, the second being the decimal point character, e.g. ['.',',']
Hash with the following elements (this is compatible with NumeralJS)
thousands
thousands separator character. Default: ,
decimal
decimal point character. Default: .
enforceGroupSize A boolean indicating whether to support that individual groups between the thousands character are exactly 3 digits
console.log(parseDecimalNumber('12.345.678,90'));
// -> 12345678.90
options
console.log(parseDecimalNumber('12.345.678,90','.,'));
// -> 12345678.90
options
console.log(parseDecimalNumber('12.345.678,90',['.',',']));
// -> 12345678.90
options
var customSeparators = {thousands:'.',decimal:','};
console.log(parseDecimalNumber('12.345.678,90',customSeparators));
// -> 12345678.90
Returns a function that will take a string as an argument and return a float
or NaN
, just like parseDecimalNumber
.
const cldr = require('cldr');
const locale = 'de_DE';
const options = cldr.extractNumberSymbols(locale);
const parse = parseDecimalNumber.withOptions(options);
parse('123.456.789,0123'); // -> 123456789.0123
Set the default thousands and decimal characters that are used when no options are passed to parseDecimalNumber
.
var defaultSeparators = {thousands:'.',decimal:','};
parseDecimalNumber.setOptions(defaultSeparators);
console.log(parseDecimalNumber('12.345.678,90'));
// -> 12345678.90
has the same effect as parseDecimalNumber.setOptions({thousands:',',decimal:'.'};)
cldr
You can easily apply CLDR data using the cldr
package:
const cldr = require('cldr');
parseDecimalNumber(
'12.345.678,90',
cldr.extractNumberSymbols('de_DE')
);
Numeral.js is good at formatting numbers and comes with an extensive set of locale data that you can use with parse-decimal-number
.
If you use numeral
in your project, you can use their locale data as follows:
parseDecimalNumber('12.345.678,90', numeral.localeData('de').delimiters);
// -> 12345678.9
You can of course use the same data to set the default values for parse-decimal-number
:
parseDecimalNumber.setOptions(numeral.localeData('de').delimiters);
parseDecimalNumber('12.345.678,90');
// -> 12345678.9
Done :relaxed:
To keep this project as small and modular as possible, the locale data itself has been left out of this library. If you need locale date, other projects might be helpful:
{%= include("tests") %}
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality, and please re-build the documentation with gulp-verb before submitting a pull request.
Andreas Pizsa (http://github.com/AndreasPizsa)
Copyright (c) 2017 Andreas Pizsa (http://github.com/AndreasPizsa), contributors.
FAQs
Parse a decimal number with i18n format support (localized decimal points and thousands separators)
The npm package parse-decimal-number receives a total of 17,755 weekly downloads. As such, parse-decimal-number popularity was classified as popular.
We found that parse-decimal-number demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.