JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old October 10th, 2004, 07:16 PM
franches franches is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 16 franches User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question round into 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> 

Reply With Quote
  #2  
Old October 11th, 2004, 03:18 AM
pocketsized pocketsized is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Brunei
Posts: 26 pocketsized User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Code:
 	function update1 ()
 	{
 	var input = document.getElementById ("work_hours").value;
 	var num = new Number (parseFloat (input));
 	var fixed = num.toFixed (2);
 	}
 

Reply With Quote
  #3  
Old October 11th, 2004, 04:10 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
alternatively, you could use:
round($variable, 2)

Reply With Quote
  #4  
Old October 11th, 2004, 11:36 PM
pocketsized pocketsized is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Brunei
Posts: 26 pocketsized User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by MadCowDzz
alternatively, you could use:
round($variable, 2)

You sure this works? The Core JS Reference doesn't mention anything about a round () function that accepts two parameters? The only round () function I could find was in the Math class and that accepts one parameter and rounds it to the nearest integer (i.e. no decimal places)

Reply With Quote
  #5  
Old October 12th, 2004, 05:50 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 5
Send a message via MSN to Spongy
round() is a PHP-function. You asked your question in a PHP-forum, so don't be surprised if you get a PHP-answer.

I suggest you use the PHP-function. JS can be turned off by users.
__________________
Work to live, don't live to work

Reply With Quote
  #6  
Old October 12th, 2004, 07:38 AM
pocketsized pocketsized is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Brunei
Posts: 26 pocketsized User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Spongy
round() is a PHP-function. You asked your question in a PHP-forum, so don't be surprised if you get a PHP-answer.

I suggest you use the PHP-function. JS can be turned off by users.

D'oh! I took a look at the original code and thought that the person asking wanted a JS solution. Yeah, I'd actually push the PHP solution since users can turn off JS.

Boy, is my face red

Reply With Quote
  #7  
Old October 12th, 2004, 08:24 PM
franches franches is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 16 franches User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Actually I did this
Quote:
function update1()
{
var workHours;
workHours = Number(document.getElementById("work_hours").value);
workHours = workHours.toFixed(2);
workHours = Math.round(workHours * Math.pow(10,2))/Math.pow(10,2);
document.getElementById("work_hours_total").value = total + workHours;
document.getElementById("remaining_hours").value = 7.5 - Number(document.getElementById("work_hours_total").value);
}

the form is ok now it already rounds off the number into 2 decimal places, however, my database stores the number I input with 3 decimal places.

Reply With Quote
  #8  
Old October 13th, 2004, 01:28 AM
franches franches is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 16 franches User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile

thanks for all your responses.

Reply With Quote
  #9  
Old October 13th, 2004, 01:21 PM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 330 Viper_SB User rank is Private First Class (20 - 50 Reputation Level)Viper_SB User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 4 h 51 m 6 sec
Reputation Power: 5
moved to JavaScript Development should have been moved before

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > round into 2 decimal places


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway