简体   繁体   English

Javascript:将字符串转换为整数然后再转换回字符串的最有效方法

[英]Javascript: Most Efficient way to convert string to integer then back to string

I have a text input field which has a value of text to take in a string like "$2,000." 我有一个文本输入字段,其中的文本值用于接收“ $ 2,000”之类的字符串。 In my functionality, I need to convert this back to aa number to run some mathematical functions and then spit it out back as another dollar value, which will be formatted like "$2,500.56" (IE not "$2,500.567"). 在我的功能中,我需要将其转换回一个数字以运行一些数学函数,然后将其吐出为另一个美元值,其格式应为“ $ 2,500.56”(即不是“ $ 2,500.567”)。 Here's the two tests I've run so far: 到目前为止,这是我进行的两项测试:

var amount = "$2,000.58"
// "2000.58"
var amount_no_sym = amount.replace(/[^\d\.]/g, '');
//2000.58
var amount_integer = parseFloat(amount_no_sym);
//2000.58 (will cut out any additional decimal places)
var amount_decimals = amount_integer.toFixed(2);
//Final output is "$2,000.58" - the toLocaleString doesn't add back the , here?
var amount_dollar_string = "$" + amount_decimals.toLocaleString();


var amount = "$2,000.58"
// "2000.58"
var amount_no_sym = amount.replace(/[^\d\.]/g, '');
// 2000.58
var amount_integer = parseFloat(amount_no_sym);
//Final output is "$2,000.58"- but sometimes it will be something like "$3,564.345" for certain calculations.
var amount_dollar_string = "$" + amount_integer.toLocaleString();

Would the most optimal solution be to go to the second one, and then write a function to process a string and cut off the last number after the decimal if there are more than two....? 最佳的解决方案是转到第二个,然后编写一个函数来处理字符串,如果多于两个,则截断小数点后的最后一个数字。 Is there a simpler way and I'm doing too much work? 有没有更简单的方法,我做的工作太多了?

Thanks in advance! 提前致谢!

Don't do your own number formatting. 不要自己格式化数字。 There's an API for that. 有一个API。

 var formatter = new Intl.NumberFormat("en-us", { style: "currency", currency: "USD" }); console.log(formatter.format(2000.58)); 

In both cases you can avoid calling the function parseFloat() by using Unary + (plus) operator which attempts to convert the operand to a number, if it is not already. 在这两种情况下,都可以避免使用Unary +(plus)运算符来调用函数parseFloat() ,该运算会尝试将操作数转换为数字(如果尚未转换为数字)。 And to format currency you can also use Number.prototype.toLocaleString() passing as arguments the desired locale and an object with options: 为了格式化货币,您还可以使用Number.prototype.toLocaleString()传递所需的语言环境和带有选项的对象作为参数:

 var amount = '$2,000,344.58', amount_integer = +amount.replace(/[^\\d\\.]/g, ''), amount_dollar_string = amount_integer.toLocaleString('en-EN', { style: 'currency', currency: 'USD' }); console.log(amount_integer); console.log(amount_dollar_string); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将JavaScript集转换为字符串的最有效方法 - The most efficient way to convert a javascript set to string 将字符串转换为数字的最有效方法是什么? - What is the most efficient way to convert a string to a number? 在Javascript中解析复杂字符串的最有效方法 - Most efficient way to parse a complex string in Javascript 在javascript中将整数转换为数字的小数部分的最有效方法是什么? - What is the most efficient way to convert an integer to the fractional part of a number in javascript? 使用JavaScript评估字符串是否是回文符的最有效方法是什么? - What's the most efficient way to evaluate if a string is a palindrome using Javascript? 使用 javascript 呈现字符串模板的最有效方法 - Most efficient way for rendering string templates using javascript 在Java中通过字符串的最有效方式是什么? - What is the most efficient way to go through string's chars in Javascript? 返回包含字符串的嵌套数组的最有效方法 (JavaScript) - Most efficient way to return a nested array that includes a string (JavaScript) 比较两个字符串数组Javascript的最快/最有效的方法 - Fastest / most efficient way to compare two string arrays Javascript '+'或parseInt()-在javascript中将字符串转换为整数的效率很高 - '+' or parseInt() - which one is efficient to convert string to integer in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM