开发者问题收集

在 javascript 中将数字格式化为货币字符串?

2018-01-22
62

我想用 JavaScript 格式化价格。

-> 250012

应该是

-> 2.500,12

查看

 <div class="form-group">
        @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
        </div>
    </div>
1个回答
> const number = 250012;
> (number/100).toLocaleString('de-DE')
< "2.500,12"

请参阅 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString 了解浏览器支持情况及更多信息

Mico
2018-01-22