2013-06-22 2 views
0

아래의 간단한 테스트 프로그램을 참조하십시오. 경고에서 두 개의 십진수로 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> 

답변

1

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

1

내 생각에 echo은 실제로 2.00을 울리지 않고 대신 2 개만 표시되므로 추측 해 볼 수 있습니다. 아마도 관련성이 있습니다 : http://php.net/manual/en/function.number-format.php

JavaScript의 관점에서 보면 toFixed 방법을 사용해보십시오.

MaxPrice = parseFloat(MaxPrice).toFixed(2); 
0

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