我想知道,如果我使用货币转换器网络服务并使用 javascript 显示结果,那么我如何才能将结果 trim 到小数点后两位。? 我已经尝试过
success: function(data) {
$('#currency_converter_result').html(data.d.toFixed(2));
}
但它没有显示结果。请指出我哪里错了?谢谢 这是我正在使用的 javascript:
<script type="text/javascript">
$(function()
{
$('#btnConvert').click(function()
{
var amount = $('#txtAmount').val();
var from = $('#ddlfrom').val();
var to = $('#ddlto').val();
$.ajax(
{
type: "POST",
url: "WebService.asmx/CurrencyConversion",
data: "{amount:" + amount + ",fromCurrency:'" + from + "',toCurrency:'" + to + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
$('#currency_converter_result').html(data.d);
}
}
}
}
</script>
最佳答案
本质上,您可以将货币视为一个字符串,只需删除小数点索引 + 2 之后的所有字符即可。
例如:
var precision = 2;
var currency = "$100.4996";
var trimmed = currency.slice( 0, currency.lastIndexOf(".") + 1 + precision );
// The additional 1 is to start the slice after the decimal point.
引用文献 -
关于javascript - 削减使用货币转换器 Web 服务产生的结果的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19268889/