# Thread: How to round variables up to 2 decimal places?

1. Registered User
Join Date
Jan 2010
Posts
8

## How to round variables up to 2 decimal places?

Hi

My javascripting is getting there, I have been learning for the last week and I have produced a working script however im getting stuck with how you round a variable (stored user input) up to the nearest whole number to 2 decimals

I have looked at many tutorials and I think that the math.round has to be used somewhere but I cannot work out the syntax to integrate it into my code because everywhere I have looked seems to be using document.write syntax:

Code:
<html>
<script language="javascript">
function measurements()
{
var val1 = parseInt(document.getElementById("value1").value);
var val2 = parseInt(document.getElementById("value2").value);
var val3 = parseInt(document.getElementById("value3").value);
ans1.value = (val1*val2) /10000;
ans2.value = (val1*val2) /10000 *2;
ans3.value = (val1*val2) /10000 *2 * val3 *1.05;
}
</script>
<body>
<h1>Calculating plain roof tiles</h1>
<p>Please use our online calculator to calculate an estimate of how many plain roof tiles you need for the whole of a gable to gable roof. The calculator makes three measurements to calculate the amount needed:</p>
<h3><strong>Plain roof tile calculator</strong></h3>
<p> </p>
Enter length of the roof (eaves length): <input type="text" id="value1" name="value1" size = 7 value""/>cm
<P>
Enter length of the gable end roof verge: <input type="text" id="value2" name="value2" size = 5 value""/>cm
<P>
How many plain roof tiles per square metre: <input type="text" id="value3" name="value3" size = 1 value""/>
<P>
<input type="button" name="Submit" value="Calculate tiles needed" onclick="javascript:measurements()"/>
<P>
Half of the roof area is <input type="text" id="answer" name="answer" size = 1 value"/> m2. The whole roof area is <input type="text" id="answer2" name="answer2" size = 1 value"/> m2
<P>
<h4><strong>The whole roof:</strong></h4>
<B>
<P>
Approximately <input type="text" id="answer3" name="answer3" size = 2 value"/> plain roof tiles are needed for this type of roof (including 5% wastage)</B>
<P>
Please note that this is an approximate estimation based on a mathematical formula. If you need a more accurate figure we recommend that you carry out your own independent calculation
<P>
</body>
</html>
Could anyone help me\give me pointers with how I can achieve rounding up to two decimal places for ans1,2 and 3?

Thanks in advance, this is a great forum and have learnt so much already

Jamie

2. Registered User+
Join Date
Feb 2006
Posts
2,930
ans1.value = (val1*val2/10000).toFixed(2);
If you want to round it up and have '.00' tacked on the end, use
ans1.value = Math.ceil(val1*val2/10000).toFixed(2);

3. Opera forever!
Join Date
Oct 2003
Location
Andover, UK
Posts
950
Harder than you'd imagine. One of JS's weakness is its lack of inbuilt decimal place rounding function. Here's how you do it:

Code:
var myNum = 1.32531;
var myNumToTwoDecimalPlaces = Math.round(myNum*Math.pow(10,2))/Math.pow(10,2);
(2 refers to the decimal places desired)

4. Registered User+
Join Date
Aug 2007
Posts
3,767
toFixed and toPrecision are the built in methods, to decimal places and significant figures respectively. The disadvantage with them is they don't remove trailing zeros, but you can either use the above solution, or a regular expression to remove them if necessary.

5. Registered User
Join Date
Jan 2010
Posts
8
Thanks for the replies, it has worked like a charm!

Regards

Jamie

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

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

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.15147 seconds
• Memory Usage 2,877KB
• Queries Executed 13 (?)
Template Usage (33):
• (2)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (5)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (5)postbit
• (5)postbit_onlinestatus
• (5)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete