How to convert String to Number according to locale (opposite .toLocaleString)?

If I do this:

var number = 3500; alert(number.toLocaleString("hi-IN")); 

I will get ३,५०० in Hindi.

But how can I convert it back to 3500 . I need something like:

 var str='३,५००'; alert(str.toLocaleNumber("en-US")); 

So, he can give 3500 .

Is this possible with javascript, jquery or any other free library?

+32
javascript
Sep 03 '14 at 13:05
source share
5 answers

I think you are looking for something like:

https://github.com/jquery/globalize

The link above will take you to the git project page. This is the js library provided by Microsoft. You should give it one try and try to use the formt method of this plugin. If you want to learn this plugin, here is a link to it:

http://weblogs.asp.net/scottgu/jquery-globalization-plugin-from-microsoft

I hope this is what you are looking for and will solve your problem soon. If this does not work, let me know.

+5
Sep 24 '14 at 12:23
source share
— -

Unfortunately, you will have to manually solve the localization. Inspired by this answer , I created a function that will manually replace Hindi numbers:

 function parseHindi(str) { return Number(str.replace(/[०१२३४५६७८९]/g, function (d) { return d.charCodeAt(0) - 2406; }).replace(/[०१२३४५६७८९]/g, function (d) { return d.charCodeAt(0) - 2415; })); } alert(parseHindi("३५००")); 

Spell here: http://jsfiddle.net/yyxgxav4/

+1
Sep 03 '14 at 16:52
source share

You can try this

 function ConvertDigits(input, source, target) { var systems = { arabic: 48, english: 48, tamil: 3046, kannada: 3302, telugu: 3174, hindi: 2406, malayalam: 3430, oriya: 2918, gurmukhi: 2662, nagari: 2534, gujarati: 2790, }, output = [], offset = 0, zero = 0, nine = 0, char = 0; source = source.toLowerCase(); target = target.toLowerCase(); if (!(source in systems && target in systems) || input == null || typeof input == "undefined" || typeof input == "object") { return input; } input = input.toString(); offset = systems[target] - systems[source]; zero = systems[source]; nine = systems[source] + 9; for (var i = 0 ; i < input.length; i++) { var char = input.charCodeAt(i); if (char >= zero && char <= nine) { output.push(String.fromCharCode(char + offset)); } else { output.push(input[i]); } } return output.join(""); 

}

var res = ConvertDigits ('12345678 9', 'hindi', 'english');

I got it here. If you need a jquery thing, please try this link.

+1
Sep 19 '14 at 7:22
source share

Use the globalization library .

Install it

npm install globalize cldr-data --save

then

 var cldr = require("cldr-data"); var Globalize = require("globalize"); Globalize.load(cldr("supplemental/likelySubtags")); Globalize.load(cldr("supplemental/numberingSystems")); Globalize.load(cldr("supplemental/currencyData")); //replace 'hi' with appropriate language tag Globalize.load(cldr("main/hi/numbers")); Globalize.load(cldr("main/hi/currencies")); //You may replace the above locale-specific loads with the following line, // which will load every type of CLDR language data for every available locale // and may consume several hundred megs of memory! //Use with caution. //Globalize.load(cldr.all()); //Set the locale //We use the extention u-nu-native to indicate that Devanagari and // not Latin numerals should be used. // '-u' means extension // '-nu' means number // '-native' means use native script //Without -u-nu-native this example will not work //See // https://en.wikipedia.org/wiki/IETF_language_tag#Extension_U_.28Unicode_Locale.29 // for more details on the U language code extension var hindiGlobalizer = Globalize('hi-IN-u-nu-native'); var parseHindiNumber = hindiGlobalizer.numberParser(); var formatHindiNumber = hindiGlobalizer.numberFormatter(); var formatRupeeCurrency = hindiGlobalizer.currencyFormatter("INR"); console.log(parseHindiNumber('३,५००')); //3500 console.log(formatHindiNumber(3500)); //३,५०० console.log(formatRupeeCurrency(3500)); //₹३,५००.०० 

https://github.com/codebling/globalize-example

+1
Aug 08 '16 at 6:15
source share
 var number = 3500; var toLocaleString = number.toLocaleString("hi-IN") var formatted = toLocaleString.replace(',','') var converted = parseInt(formatted) 
0
Jul 28 '19 at 19:15
source share



All Articles