# Thread: Javascript Round Up 2 decimal places

1. Registered User
Join Date
Nov 2011
Posts
4

## Javascript Round Up 2 decimal places

Hi,

I am trying to use the following javascript (http://people.w3.org/~dsr/forms-lite/forms-lite.js) to create something similar to the example on (http://people.w3.org/~dsr/forms-lite/3/index.html) for a school project but I wondered if someone could advise me how I can round numbers to 2 decimal places in the field box as I have to convert the calculated value by an exchange rate, e.g.:

where x=100 and y=50

calculate="(x+y)/1.55"

returns a value of 96.77419355. I would like to round this figure up to 2 decimal places so it would read 96.77 but I cannot figure out how to do this.

Your help would be greatly appreciated.

Regards

John

2. use the method toFixed()

Keep in mind that this method returns a String!

3. Registered User
Join Date
Nov 2011
Posts
4
Thanks for your quick reply. Is there any way I can return the rounded figure into a field? e.g. in the example on the link above I want the rounded figure to appear in the f3 field (X+Y).

Sorry if this is very basic javascript. I am new to JS do I am still learning.

Thanks.

4. Code:
`document.getElementById('f3').value=((x+y)/1.55).toFixed(2);`

5. Registered User
Join Date
Nov 2011
Posts
4
Okay. Like this?

Code:
```<script type="text/javascript">
document.getElementById('f3').value=((x+y)/1.55).toFixed(2);
</script>
<form name="form1" onsubmit="false">
<legend>Simple calculation</legend>
<label for="f1">X</label>
<input id="f1" name="a" datatype="number"/>
<label for="f2">Y</label>
<input id="f2" name="b" datatype="number"/>
<label for="f3">X + Y</label>
<input id="f3" name="sum" calculate="x+y" datatype="number" />
</form>```
As I am new to JS I do not exactly know how to put all this together. Sorry to be a pain.

6. No. JavaScript uses methods. Methods are nested in functions. Functions are triggered by events, following the user's actions.

So, you need an action: the user clicks a button. The associated JavaScript event is onclick. That event should fire a function. All the statements, expressions, calculations, etc, should, usually, be nested inside functions:
Code:
```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function calculate(){
var x=Number(document.getElementById('f1').value);
var y=Number(document.getElementById('f2').value);
document.getElementById('f3').value=((x+y)/1.55).toFixed(2);
}
</script>
<body>
<form action=-"">
<legend>Simple calculation</legend>
<label for="f1">X</label>
<input type="text" id="f1">
<label for="f2">Y</label>
<input type="text" id="f2">
<label for="f3">X + Y / 1.55 = </label>
<input type="button" value="Calculate" onclick="calculate()">
</form>
</body>
</html>```

7. Registered User
Join Date
Nov 2011
Posts
4
Fantastic! Worked a treat. Thank you so much for all your help.

Jord

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.14033 seconds
• Memory Usage 2,909KB
• Queries Executed 15 (?)
Template Usage (33):
• (3)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (7)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (7)postbit
• (7)postbit_onlinestatus
• (7)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