All Projects → AndreasPizsa → Parse Decimal Number

AndreasPizsa / Parse Decimal Number

Licence: mit
🏧 Parse a decimal number with i18n format support (localized decimal points and comma separators)

Programming Languages

javascript
184084 projects - #8 most used programming language
coffeescript
4710 projects

Projects that are alternatives of or similar to Parse Decimal Number

Js Lingui
🌍📖 A readable, automated, and optimized (5 kb) internationalization for JavaScript
Stars: ✭ 3,249 (+9745.45%)
Mutual labels:  i18n, localization
Easy localization
Easy and Fast internationalizing your Flutter Apps
Stars: ✭ 407 (+1133.33%)
Mutual labels:  i18n, localization
Gatsby Plugin Intl
Gatsby plugin that turns your website into an internationalization-framework out of the box.
Stars: ✭ 300 (+809.09%)
Mutual labels:  i18n, localization
Next Translate
Next.js plugin + i18n API for Next.js 10 🌍 - Load page translations and use them in an easy way!
Stars: ✭ 867 (+2527.27%)
Mutual labels:  i18n, localization
Fluent.js
JavaScript implementation of Project Fluent
Stars: ✭ 622 (+1784.85%)
Mutual labels:  i18n, localization
Tower
i18n & L10n library for Clojure/Script
Stars: ✭ 264 (+700%)
Mutual labels:  i18n, localization
React Localize Redux
Dead simple localization for your React components
Stars: ✭ 384 (+1063.64%)
Mutual labels:  i18n, localization
python-fluent
Python implementation of Project Fluent
Stars: ✭ 142 (+330.3%)
Mutual labels:  i18n, localization
Fluent Rs
Rust implementation of Project Fluent
Stars: ✭ 503 (+1424.24%)
Mutual labels:  i18n, localization
Laravel Js Localization
🌐 Convert your Laravel messages and consume them in the front-end!
Stars: ✭ 451 (+1266.67%)
Mutual labels:  i18n, localization
Mojito
An automation platform that enables continuous localization.
Stars: ✭ 256 (+675.76%)
Mutual labels:  i18n, localization
Frenchkiss.js
The blazing fast lightweight internationalization (i18n) module for javascript
Stars: ✭ 776 (+2251.52%)
Mutual labels:  i18n, localization
labels
Bolt Labels extension - Translatable labels for Bolt
Stars: ✭ 18 (-45.45%)
Mutual labels:  i18n, localization
Eo Locale
🌏Internationalize js apps 👔Elegant lightweight library based on Internationalization API
Stars: ✭ 290 (+778.79%)
Mutual labels:  i18n, localization
i18n
Package i18n is for app Internationalization and Localization.
Stars: ✭ 79 (+139.39%)
Mutual labels:  i18n, localization
Androidlocalizeplugin
🌏 Android localization plugin. support multiple languages, no need to apply for key.
Stars: ✭ 352 (+966.67%)
Mutual labels:  i18n, localization
plate
Internationalization library for Python
Stars: ✭ 31 (-6.06%)
Mutual labels:  i18n, localization
i18n-tag-schema
Generates a json schema for all i18n tagged template literals in your project
Stars: ✭ 15 (-54.55%)
Mutual labels:  i18n, localization
Juliazh.jl
Julia语言中文文档
Stars: ✭ 425 (+1187.88%)
Mutual labels:  i18n, localization
Mobility
Pluggable Ruby translation framework
Stars: ✭ 644 (+1851.52%)
Mutual labels:  i18n, localization

parse-decimal-number Travis Coverage Status Downloads

FOSSA Status

Parse a decimal number with i18n format support (localized decimal points and thousands separators)

About

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:

  • Simple. String in, float out, done. ✓
  • Accurate. Parses numbers and returns NaN for non-numbers. (=good for input validation) ✓
  • Lightweight. (<1k minified) ✓
  • Complete. No external dependencies ✓
  • Solid. 100% Code Coverage ✓
  • CLDR Support. Supports cldr data ✓

In it’s simplest form, you just use it as a parseFloat replacement.

Install

Install with npm

npm i parse-decimal-number --save

Usage

parseDecimalNumber = require('parse-decimal-number');
console.log(parseDecimalNumber('12,345,678.90'));
// -> 12345678.90

console.log(parseDecimalNumber('12.345.678,90','.,'));
// -> 12345678.90

parseDecimalNumber(string [,options])

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.

Parameters

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

Examples

console.log(parseDecimalNumber('12.345.678,90'));
// -> 12345678.90
String options
console.log(parseDecimalNumber('12.345.678,90','.,'));
// -> 12345678.90
Array options
console.log(parseDecimalNumber('12.345.678,90',['.',',']));
// -> 12345678.90
Hash options
var customSeparators = {thousands:'.',decimal:','};
console.log(parseDecimalNumber('12.345.678,90',customSeparators));
// -> 12345678.90

parseDecimalNumber.withOptions(options)

Returns a function that will take a string as an argument and return a float or NaN, just like parseDecimalNumber.

Example

  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

Setting and Resetting Default Options

parseDecimalNumber.setOptions

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
parseDecimalNumber.factorySettings

has the same effect as parseDecimalNumber.setOptions({thousands:',',decimal:'.'};)

Using with cldr

You can easily apply CLDR data using the cldr package:

  const cldr = require('cldr');

  parseDecimalNumber(
    '12.345.678,90',
    cldr.extractNumberSymbols('de_DE')
  );

Using with Numeral.js

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 ☺️

Related Projects

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:

Running tests

{%= include("tests") %}

Contributing

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.

Author

Andreas Pizsa (http://github.com/AndreasPizsa)

License

Copyright (c) 2017 Andreas Pizsa (http://github.com/AndreasPizsa), contributors.

FOSSA Status Greenkeeper badge

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].