...

View Full Version : round 2 decimal places



franches
10-11-2004, 01:52 AM
hi,
i was having this problem on how to round a number into 2 decimal places. I have read some of the tutorials but with my case I don't know how to do it. i hope someon could help with this one. all the 3 input box should have 2 decimal places only.

thank you in advance.



<head>
<script type="text/javascript">
var total = <?php echo (isset($_POST['work_hours_total']) ? $_POST['work_hours_total'] : "0.00"); ?>;

function update1()
{
document.getElementById("work_hours_total").value = total + Number(document.getElementById("work_hours").value);
document.getElementById("remaining_hours").value = 7.5 - Number(document.getElementById("work_hours_total").value);
}
</script>
</head>
<body>
<form name="log" action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<div>
<label for="work_hours">Work Hours</label>
<input id="work_hours" name="work_hours" type="text" size="5" value= "0.00" onkeypress = "return onkeypressWrkHrsCheck()" onkeyup = "return onkeyupWrkHrsCheck()" onchange="update1()">
</div>
<div>
<label for="work_hours_total">Total Work Hours</label>
<input id="work_hours_total" name="work_hours_total" type="text" size="5" value="<?php echo (isset($_POST['work_hours_total']) ? $_POST['work_hours_total'] : "0.00"); ?>" readonly="true">
</div>
<div>
<label for="remaining_hours">Hours Remaining</label>
<input id="remaining_hours" name="remaining_hours" type="text" size="5" value="<?php echo (isset($_POST['work_hours_total']) ? (7.5 - $_POST['work_hours_total']) : "7.50"); ?>" readonly="true">
</div>
<div>
<input type="submit" value="Add hours" name="Add">
</body>

hemebond
10-11-2004, 02:00 AM
http://us2.php.net/manual/en/function.round.php

franches
10-11-2004, 03:15 AM
anyone who has a code on my problem? I read the manual but still I don't know how to do it with my code. I am on the process of learning php. :confused:

firepages
10-11-2004, 05:04 AM
e.g.


<?php echo (isset($_POST['work_hours_total']) ? round($_POST['work_hours_total'],2) : "0.00"); ?>

franches
10-11-2004, 07:12 AM
e.g.


<?php echo (isset($_POST['work_hours_total']) ? round($_POST['work_hours_total'],2) : "0.00"); ?>


i tried this one but it didn't work. i tried to input with 3 decimal places and it still displays with 3 decimal places.

franches
10-11-2004, 10:08 AM
i've encountered this problem when i tried to put this

<label for="work_hours">Work Hours</label>
<input id="work_hours" name="work_hours" type="text" size="5" value= "<?php echo number_format(round($_POST['work_hours'],2),2) ; ?>" onkeypress = "return onkeypressWrkHrsCheck()" onkeyup = "return onkeyupWrkHrsCheck()" onchange="update1()">

after pressing the submit button. input box displays the number i've just input (but with 2 decimal places only) which I don't want to happen. what i would like to do is after pressing the submit button the input box will display "0.00". and also when i check my database the number i've tried to input with 3 decimal places are not changed into 2 decimal places. i don't know what to do anymore. i'm stuck with this problem for 2 days. i really need help.:confused:

firepages
10-11-2004, 10:17 AM
use round($value,2) OR number_format($val,2) not both !

OK your question appears to have changed ,
I think you are saying that you want your database to record all decimal spaces ? pease confim.

If so then your database field needs be set to the appropriate type , e.g. in MySQL the field needs to be of (example only) DECIMAL (10,5) which would allow a max number of 99999.99999 , (10,2) would allow 99999999.99 etc

franches
10-12-2004, 03:36 AM
in this form i am thinking that sometimes the user accidentally input a number with more than 2 decimal places. so maybe i think that it is better that i have in my code that would change it into 2 decimal places only when it is submitted into my database.

the other thing is after submitting the form the input box will have a value of "0.00" which is ready for the next input.

i have attached my code here.

any help would be greatly appreciated.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum