简体   繁体   English

Javascript 如何将整数格式化为货币字符串?

[英]Javascript How To Format An Integer As A Currency String?

I have an integer stored as US cents, ie 1700 cents.我有一个整数存储为美分,即 1700 美分。 How can I convert that to a string that is $17.00 using javascript?如何使用 javascript 将其转换为 $17.00 的字符串? I have tried toFixed and Intl.NumberFormat but they return $1700?我尝试过toFixedIntl.NumberFormat但它们返回 1700 美元?

You can use toLocaleString() function for this.您可以为此使用toLocaleString()函数。

 var cents = 1629; var dollars = cents / 100; dollars = dollars.toLocaleString("en-US", {style:"currency", currency:"USD"}); console.log(dollars);

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

相关问题 如何在 JavaScript 中格式化货币? - How to format currency in JavaScript? 在 AutoNumeric javascript 中,如果输入是 integer,如何创建显示 integer 的格式,如果输入是浮点数,如何创建显示货币的格式? - In AutoNumeric javascript, how do I create a format that shows integer if the input is an integer but currency if the input is a float? 如何使用货币和语言环境格式化 JavaScript 中的大数字(由字符串表示)? - How to format a big number (represented by string) in JavaScript with currency and with respect to the locale? 如何在 Laravel 中将输入格式化为货币并另存为 Integer? - How to Format Input as Currency and Save as Integer in Laravel? 如何使用 javascript 格式化货币并进行验证 - How format currency with javascript and validate 如何在javascript中格式化货币和时间 - How to format currency and time in javascript 我不想将一串数字格式化为 javascript 中的货币,但我不知道如何格式化我的字符串 - I wan't to format a string of numbers as a currency in javascript but i don't know how to format my string 如何将字符串货币值转换为整数 - How to convert string currency value in integer 如何在JavaScript中获取字符串的货币 - How to get currency of a string in javascript 在 Javascript 中将货币转换为整数 - Turn Currency into Integer in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM