Javascript数字格式显示

标签 javascript formatting numeric

请参阅下面的简单测试程序。在警报中,我需要显示带有两位小数的变量 MaxPrice。我已将 $Maxprice 放入测试程序中,但实际上,$MaxPrice 是从 SQL 文件中读取的,并且是一个具有数值的数字字段(在本例中为 2.00)。警报仅显示“2”。如何让它显示“2.00”?

<html>
<head>
<script language="javascript">
function myFunction()
{
PriceEntered=document.forms["form1"]["Price"].value;
MaxPrice=document.forms["form1"]["MaxPrice"].value;
alert("Price Entered=" + PriceEntered + " and maximum= " +  MaxPrice );
}
</script>
</head>

<?php
$MaxPrice=2.00;
?>

<body>
<form id="form1" name="form1" method="post" onsubmit="myFunction ()" action="test.php">
<input name="MaxPrice" id="MaxPrice" type="hidden" value="<?php echo $MaxPrice;?>">
<input name="Price" id="Price" type="text" value="3.00">
<input type="submit" name="submit1" value"Submit" />
</form>
</body>

最佳答案

使用 alert("Price Entered="+ Number(PriceEntered).toFixed(2) + "和 max= "+ Number(MaxPrice).toFixed(2));

关于Javascript数字格式显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17254976/

相关文章:

windows - Windows 8 应用程序中 Numericbox 的替代品?

JavaScript getElementById 空值错误以及如何避免它

Ruby:将 HTML/Redcloth 转换为纯文本

android - 使用数据绑定(bind)时从 getTimeInMillis 获取日期

oracle - 在 SQLPlus 中格式化查询的输出

javascript - 使用 JS 获取希伯来历日期

JavaScript 重写 href 链接

javascript - Ajax发送数据到 Controller 时为空

iOS,核心数据。从表中请求所有以数字符号开头的记录而不进行额外的数据处理?