Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2004
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    round 2 decimal places

    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.

    PHP Code:
    <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>

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts

  • #3
    New Coder
    Join Date
    Aug 2004
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    e.g.
    PHP Code:
    <?php echo (isset($_POST['work_hours_total']) ? round($_POST['work_hours_total'],2) : "0.00"); ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New Coder
    Join Date
    Aug 2004
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages
    e.g.
    PHP Code:
    <?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.

  • #6
    New Coder
    Join Date
    Aug 2004
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    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
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    New Coder
    Join Date
    Aug 2004
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    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.
    Attached Files Attached Files


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •