Forum Moderators: coopster

Message Too Old, No Replies

round into 2 decimal places

         

franches

12:10 am on Oct 11, 2004 (gmt 0)

10+ Year Member



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.

[edited by: ergophobe at 1:00 am (utc) on Oct. 11, 2004]
[edit reason] unecessary code dump snipped as per forum charter [/edit]

Birdman

3:13 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

Here is the function I use:

function Money($float) {

return sprintf("%01.2f", $float);

}

Birdman

franches

5:24 am on Oct 11, 2004 (gmt 0)

10+ Year Member



[quote]<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()">[quote]

This is my input box. This is where the user will input the number. all the functions are working. and i would like to add that the input should only be in 2 decimal places when it is submitted to the database while the excess decimal places will be ignored.

i will be waiting for your response.

mincklerstraat

7:59 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



franches, you're doing php output here with a bunch of javascript in there, and I get the feeling you're still pretty much struggling with php. Why don't you try to sort of simplify things? It's very likely that this scenario will cause a lot of confusion for you and won't help you so much in learning php. Of course, I might be wrong - I just know that if when I was trying to learn a lot of this stuff, if I were involved with code as complicated as that, I probably wouldn't have learned as fast.

If you don't understand Birdman's answer, you can also look at the function round [be2.php.net].

franches

8:13 am on Oct 11, 2004 (gmt 0)

10+ Year Member



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:

Birdman

10:59 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok,

In the PHP script that the form submits to, put the function(Money()) in it. Then, when you add to the database, use the function.

Example:

$work_hrs = Money($_POST['work_hours']);

Now $work_hrs only has two digits after the decimal.

Birdman

Ps: Here are a couple JavaScript functions as well.

function formatAsMoney(mnt) {
mnt -= 0;
mnt = (Math.round(mnt*100))/100;
return (mnt == Math.floor(mnt))? mnt + '.00'
: ( (mnt*10 == Math.floor(mnt*10))?
mnt + '0' : mnt);
}

function numOnly(val,el){
if (isNaN(val)){
alert("Only numbers in this field please.");
document.forms[0].elements[el].value = "";
return;
}
}

franches

2:27 am on Oct 12, 2004 (gmt 0)

10+ Year Member



Thanks but I am having a problem. I don't know where in my code I will put this. Please guide me with this one. I want to attach my code for you to examine it but there's no way I can attach it.

franches

6:14 am on Oct 13, 2004 (gmt 0)

10+ Year Member



by the way, i figured it out.

thank you very much for helping me.

SofterLogic UK

11:38 am on Oct 13, 2004 (gmt 0)

10+ Year Member



I tend to use 0.01*round($number*100);