2013-06-22 17 views
0

請參見下面的簡單的測試程序。在警報中,我需要用兩位小數顯示變量MaxPrice。我在測試程序到位$ Maxprice,但在現實中,$ MaxPrice從SQL文件中讀取,並與一個數值(在這種情況下爲2.00)的數字字段。警報僅顯示「2」。我如何讓它顯示「2.00」?的Javascript數字格式顯示

<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> 

回答

1

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

0

試試這個

var num = 2; 
alert(num.toFixed(2));