# Thread: Rounding answer DOWN to 2 decimal places / use dollar sign

1. Registered User
Join Date
Jul 2003
Location
Louisiana
Posts
327

## Rounding answer DOWN to 2 decimal places / use dollar sign

Here is my code:

Code:
```<script type="text/javascript" language="Javascript">

var v1 = 0;
var v2 = .00939740;
var answer = 0;

function calculate () {
// The Calculation: change operator in this equation to change the calculation
answer = v1 * v2;

var elem;
elem = document.getElementById("v1");
elem.value = v1;
elem = document.getElementById("v2");
elem.value = v2;

}

function setValue(elem) {
var val = 0.0;
if (elem.value != "") {
val = parseFloat(elem.value);
}
else {
val = 0.0;
}
switch (elem.id) {
case "v1": v1 = val; break;
case "v2": v2 = val; break;
}
calculate();
}
</script>```
Right now, it displays my answer as a 15 decimal place number. I want it to be a 2 decimal place number and I want it to round down.

And, is there a way to have it display with a dollar sign in front?

Lastly, is there a way to have it display with the answer yellow highlighted?

(I am not a programmer, I just make do!)
Last edited by Duke Will; 04-29-2012 at 02:04 PM.

2. Try using the forum's search facility, using words like 'rounding' or 'decimal places'. We don't need to answer this one again.

3. Registered User
Join Date
Nov 2010
Posts
1,276
FYI, it's a very bad idea to have variables and element ID's sharing the same name. It makes IE freak out.

Code:
```<script type="text/javascript">

var val = 5;
var val2 = .00939740;
var ans = 0;

function calculate () {
// The Calculation: change operator in this equation to change the calculation
ans = val * val2;

var elem;
document.getElementById("v1").value = val;
document.getElementById("v2").value = val2;

}

function setValue(elem) {
var val = 0.0;
if (elem.value != "") {
val = parseFloat(elem.value);
}
else {
val = 0.0;
}
switch (elem.id) {
case "v1": v1 = val; break;
case "v2": v2 = val; break;
}
calculate();
}
</script>```

4. Registered User
Join Date
Jul 2003
Location
Louisiana
Posts
327
I want to thank you for helping this non-programmer! However, it works but not quite.

Here is my original page with the code I had...

http://tinyurl.com/april29-old

Here is the new page with your code...

http://tinyurl.com/april29-new

Note that all that is to happen is the web visitor is going to input a number into box 1. Note that it shows "0" but when you type any number, the zero disappears and the new number takes its place. Seems like a good way.

With your amended code, all appears very cool except I cannot change that original "5" number, it is hard-wired in and won't change. I even changed your first line to be...

var val = 0;

...but that didn't work either. Thanks if you can help.

5. Registered User
Join Date
Nov 2010
Posts
1,276
I have very little idea what's supposed to be going on there, but if you want a text input to start off with a value why not just write it that way?

Code:
`<input type="text" id="v1" value="0">`

6. Registered User
Join Date
Nov 2010
Posts
1,276
I think I get it now... does this work better?

Code:
```<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>

Enter VIP points
<input type="text" id="v1" onkeyup="calculate()" value="0"/><br><br>
profit share multiplier
<input type="text" id="v2" onkeyup="calculate()" value=".00939740"/><br><br>
Retail Profit Share Award

<script type="text/javascript">

function calculate () {
// The Calculation: change operator in this equation to change the calculation
document.getElementById("v1").value=Number(document.getElementById("v1").value);
ans = document.getElementById("v1").value * document.getElementById("v2").value;

if (!isNaN(ans)&&ans!=0){
} else {
}
}
</script>
</body>
</html>```

7. Registered User
Join Date
Jul 2003
Location
Louisiana
Posts
327
xelawho, if I input something other than a number into Box 1, I get "NaN" (which I assume is for "not a number"... and even reloading the page won't fix it so I can enter a number. Thanks.

8. Registered User
Join Date
Nov 2010
Posts
1,276
yeah, I was just looking at that... how about this...

Code:
```function calculate () {
// The Calculation: change operator in this equation to change the calculation
document.getElementById("v1").value=document.getElementById("v1").value.replace(/[^0-9]/, '')*1

ans = document.getElementById("v1").value * document.getElementById("v2").value;

if (!isNaN(ans)&&ans!=0){
} else {
}
}```

9. A similar function that should give the same results...

Code:
```function calculate()
{
var
v = parseInt(0 + document.getElementById('v1').value, 10) * parseFloat(0 + document.getElementById('v2').value, 10),

e.value = v ? '\$' + v.toFixed(2) : '';
e.style.backgroundColor = v ? 'yellow' : 'white';
}```

10. Registered User
Join Date
Jul 2003
Location
Louisiana
Posts
327
Thanks, got it all working like I want it now.

11. Registered User
Join Date
Jul 2003
Location
Louisiana
Posts
327
I spoke too soon. Almost there.

If someone changes Box 2 to be something other than what it was when the page first loaded, upon refresh, it does not provide the original value. How can I fix that? Upon refresh or a link or something that says "Get original value for Box 2."

EDIT: Disregard, I made it read-only like Box 3. Seems to work, guess I did it right.

EDIT: How can I make it so the visitor can enter a 2-digit decimal number in Box 1 ("v1")? Perhaps I should force them to enter it with 2 decimal places? How?

I am using this code:

Code:
```<script type="text/javascript">

function calculate () {
// The Calculation: change operator in this equation to change the calculation
document.getElementById("v1").value=document.getElementById("v1").value.replace(/[^0-9]/, '')*1
document.getElementById("v1").style.backgroundColor="yellow";
ans = document.getElementById("v1").value * document.getElementById("v2").value;
document.getElementById("v2").style.backgroundColor="red";
if (!isNaN(ans)&&ans!=0){
} else {
}
}
</script>```
See http://tinyurl.com/april29-8.
Last edited by Duke Will; 04-29-2012 at 06:28 PM.

12. Registered User
Join Date
Jul 2003
Location
Louisiana
Posts
327
I'm getting the "NaN" error for Box 1, as well. I see how correct it (like Box 2) but I'm not skilled enough to pull it off.

Also, about having the person enter a number with 2 digits, I found this page...

http://www.coderanch.com/t/114528/HT...nt-restriction

I had already tried a workaround, by asking the visitor to type \$123.45 as 12345. (And I added a "/100" to my code. But, that will probably produce too many errors. If anyone knows how to incorporate the coders comments at above link, let me know.

Currently, my (unfinished) page is...

HTML Code:
```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML LANG=en>
<TITLE>Zeek Rewards Calculator</TITLE>
<META NAME="KEYWORDS" Content="zeek rewards calculator, zeek rewards multiplier, multiplier, calculator, zeek, profit pool">
<META NAME="ROBOTS" CONTENT="NOINDEX,NOFOLLOW">

<style type="text/css">
body {font-size:100&#37;;}
h1 {font-size:2.5em;font-family:timesnewroman}
h2 {font-size:1.875em;font-family:timesnewroman}
p {font-size:1.5em;font-family:verdana}

body
{
background-color:#BDBDBD;
}
</style>
<blockquote>
<blockquote>
<form<BR /><font size=1><BR />
<h1>Zeek Rewards Calculator</h1>
<h2>To Monitor Your Results</h2>
<BR />
<p>Have you longed for a <em>Zeek Rewards calculator</em> to help you make sure your back office report is in line with what other people are being paid in the daily Retail Profits Pool? Look no more, here it is! Simply input your appropriate amount of VIP points into Box 1 and the results show in Box 3 instantly as you type.</p>
<p>
<input type="text" id="v1" onkeyup="calculate()" value="0"/> (BOX 1) -- Enter VIP points from yesterday for line item immediately prior to last night's run. Enter no decimal, i.e., \$123.45 entered as "12345" (no quotes)<BR /><BR />
<input type="text" id="v2" onkeyup="calculate()" value=".00939740" readonly="readonly"//> (BOX 2) -- Multiplier for (today), pool (yesterday), as determined by this site's webmaster.<BR /><BR />
<input type="text" id="answer" readonly="readonly"/> (BOX 3) -- Your calculated Retail Profit Share Award for today (for yesterday's business) according to the multiplier provided by this site's webmaster.<BR /><BR />
(Box 3 changes immediately as you input into Box 1.)<BR /><BR />
If you see a positive answer in Box 3 above, Zeek was profitable last night!<BR /><BR />
Should you see a discrepancy (other than a minor rounding difference), make sure you entered your correct VIP points. Please do not contact the webmaster for support; direct support calls to your sponsor. If both you and your sponsor have double-checked the calculations and feel that the multiplier is incorrect, please contact this webmaster.<BR /><BR />
TIP: Keep two browser tabs (Internet Explorer, Firefox or Chrome) open to your Zeek Rewards site and this site 24/7; don't close the tabs! You'll be visiting each day anyway! Didn't know you could do that?</p>
<p style="color:red">
NOTE: No results are guaranteed but not much in life is! As for Zeek Rewards and their Retail Profit Pool, it's very simple: If they make a profit, we can earn a share in that pool by qualifying. And qualifying is not hard! If Zeek does not make a profit on any given day, we have potential income streams other than the daily pool. Please protect your Zeek business (and mine) by getting proper training for what we can and cannot say about this revolutionary business model.</p>
<p>Best of luck to you with Zeek Rewards!</p>
<p>Legal Disclaimers</p>
<p>Income Disclosure Statement</p>
</form>
</blockquote></blockquote>
<script type="text/javascript">

function calculate () {
// The Calculation: change operator in this equation to change the calculation
document.getElementById("v1").value=document.getElementById("v1").value.replace(/[^0-9]/, '')*1
document.getElementById("v1").style.backgroundColor="yellow";
ans = document.getElementById("v1").value /100 * document.getElementById("v2").value;
document.getElementById("v2").style.backgroundColor="red";
if (!isNaN(ans)&&ans!=0){
} else {
}
}
</script>
</BODY>
</HTML>```
Also, I would rather have the calculator have a button called "Calculate" and have the operation execute only after the visitor presses the button.
Last edited by Duke Will; 04-29-2012 at 09:56 PM.

##### Users Browsing this Thread

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.15803 seconds
• Memory Usage 3,012KB
• Queries Executed 15 (?)
Template Usage (34):
• (7)bbcode_code
• (1)bbcode_html
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (12)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (12)postbit
• (12)postbit_onlinestatus
• (12)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 (72):
• 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_postinfo_query
• fetch_postinfo
• 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