Coder Social home page Coder Social logo

neybertech / fromlocalestring Goto Github PK

View Code? Open in Web Editor NEW

This project forked from harrypehkonen/fromlocalestring

0.0 1.0 0.0 56 KB

Why is there no fromLocaleString in JavaScript? I could really use it. In some locales, commas and periods have the opposite role, and trying to figure out which to use can be error-prone.

JavaScript 97.06% HTML 0.43% CSS 2.51%

fromlocalestring's Introduction

fromlocalestring

Why is there no fromLocaleString in JavaScript? I could really use it. In some locales, commas and periods have the opposite role, and trying to figure out which to use can be error-prone.

Should work in all modern browsers. With the help of https://github.com/es-shims/es5-shim, should even work in ie8 (internet explorer 8).

If you use number.toLocaleString(), you may get spaces (U+00a0), commas, periods, etc. as the thousands-separators, and a period, comma, etc. as the decimal-separator. If you want to convert back to a number, you have to figure out which separators to use. This library will figure out what those are by doing the conversion once, and detecting what characters were used.

Usage

From Node

This should change. It should live in an npm module. But it doesn't. Yet.

> var FromLocaleString = require('./fromlocalestring');
undefined
> var number = 1234;
undefined
> var txt = number.toLocaleString();
undefined
> txt
'1,234'
> var fromLocaleString = new FromLocaleString();
undefined
> fromLocaleString.number(txt);
1234
>

From a Browser

<script src="./fromlocalestring.js"></script>
<script>
var fromLocaleString = new FromLocaleString();
var number = 1234;
var txt = number.toLocaleString();
console.log(JSON.stringify(fromLocaleString.number(txt)));
</script>

... also gives you 1234.

Methods

.number

Convert using Number().

.parseFloat

Convert using parseFloat().

.parseInt

Convert using parseInt(). Using base as the second argument is supported. If you don't supply one, one is not passed to the native parseInt function.

Constants

Well, not really constants because not only are they initialized to whatever the current locale dictates, you can also change them at any time.

.separators.thousandsSeparator

Probably a comma, period, U+00a0, ...

.separators.decimalSeparator

Probably a period, comma, ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.