0

See the simple test program below. In the alert, I need to display the variable MaxPrice with two decimals. I've place $Maxprice in the test program, but in reality, $MaxPrice is read from a SQL file and is a numeric field with a numeric value (in this case 2.00). The alert is displaying only "2". How do I get it to display "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> 

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

My guess is the echo is not really echoing 2.00 but instead just 2, so you may want to look into that. Possibly relevant: http://php.net/manual/en/function.number-format.php

From the JavaScript perspective though, try the toFixed method, e.g.

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

Comments

0

try this

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.