javascript 如何根据语言环境将字符串转换为数字(与 .toLocaleString 相反)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25645163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert String to Number according to locale (opposite of .toLocaleString)?
提问by user2754545
If I do:
如果我做:
var number = 3500;
alert(number.toLocaleString("hi-IN"));
I will get ?,???
in Hindi.
我会?,???
用印地语。
But how can I convert it back to 3500
.
I want something like:
但是我怎样才能将它转换回3500
. 我想要这样的东西:
var str='?,???';
alert(str.toLocaleNumber("en-US"));
So, that it can give 3500
.
所以,它可以给3500
.
Is it possible by javascript, jquery or any other free library?
是否可以通过 javascript、jquery 或任何其他免费库实现?
回答by learner
I think you are looking for something like:
我认为您正在寻找类似的东西:
https://github.com/jquery/globalize
https://github.com/jquery/globalize
Above link will take you to git project page. This is a js library contributed by Microsoft. You should give it one try and try to use formt method of that plugin. If you want to study this plugin, here is the link for the same:
上面的链接将带你到 git 项目页面。这是微软贡献的js库。您应该尝试一下并尝试使用该插件的 formt 方法。如果你想研究这个插件,这里是相同的链接:
http://weblogs.asp.net/scottgu/jquery-globalization-plugin-from-microsoft
http://weblogs.asp.net/scottgu/jquery-globalization-plugin-from-microsoft
I hope this is what you are looking for and will resolve your problem soon. If it doesn't work, let me know.
我希望这就是您正在寻找的内容,并将很快解决您的问题。如果它不起作用,请告诉我。
回答by Codebling
Use the Globalize library.
使用全球化库。
Install it
安装它
npm install globalize cldr-data --save
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)); //??,???.??
回答by denisol
Unfortunately you will have to tackle the localisation manually. Inspired by this answer, I created a function that will manually replace the 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("????"));
Fiddle here: http://jsfiddle.net/yyxgxav4/
在这里小提琴:http: //jsfiddle.net/yyxgxav4/
回答by Satheesh
You can try this out
你可以试试这个
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('?????????', 'hindi', 'english');
var res = ConvertDigits('?????????', 'hindi', 'english');
I got it from hereIf you need a jquery thing then please try this link
回答by user11849266
var number = 3500;
var toLocaleString = number.toLocaleString("hi-IN")
var formatted = toLocaleString.replace(',','')
var converted = parseInt(formatted)