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:
  #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 14 m 9 sec
Reputation Power: 10
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: 7
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: 331 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 57 m 14 sec
Reputation Power: 7
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




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek