/    Sign up×
Community /Pin to ProfileBookmark

Math rounding decimals?

I am using alot of Javascript for my Wall Calculator project,
I needed something that would round a number to a Currency format.
It was suggested I use a “toFixed()” command.
Well it works fine and even passes the W3C Validator for HTML 4.01, but now I’ve found that some older browsers will not accept it.
I’m having a hard time finding what I need at the W3C site, anyone have any recommendations?
Here’s an example of my code:

// calculate quantity subtotal
// calculate track quantity price
f.track_count_price_temporary =
(f.track_count.value
* 10
*
track_width_price[f.stud_width.value]);
f.track_count_price.value =

f.track_count_price_temporary.toFixed(2);

reference:

[URL=http://www.epourania.com/wallcalc/wallcalc25.html]http://www.epourania.com/wallcalc/wallcalc25.html[/URL]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@CharlesSep 26.2004 — You missed something really important over there on the W3C site: [i]From the Web Content Accessibility Guidelines 1.0[/i]

[b]6.3 Ensure that pages are usable when scripts, applets, or other programmatic objects are turned off or not supported.[/b]

[i]http://www.w3.org/TR/WCAG10/wai-pageauth.html#tech-scripts[/i][/quote]
About one in ten don't use JavaScript but less than one in a hundred use JavaScript that doesn't support "Number.toFixed()" (http://www.w3.org/TR/WCAG10/wai-pageauth.html#tech-scripts). You're getting hung up on the wrong thing.
Copy linkTweet thisAlerts:
@steelersfan88Sep 26.2004 — What Charles said is true, except, a task like rounding is too light for need of server side scripting. I hate to say it, but I would recommend you inform users JS is required to use the page, or write the link to the page using JS, so users without JS do not have to visit an inacessible page.

For what you asked, the .toFixed(dec_places); method/prototype actually returns a string:<script type="text/javascript">
alert(typeof 10.25.toFixed(2)); // alerts string
</script>
To use this as a number again, you'd have to parse it to a Number, hence performing two unneccesary operations. Better way, the .round(dec_places); prototype:<script type="text/javascript">

Number.prototype.round = function(d) {
return (Math.round(this*Math.pow(10,d))/Math.pow(10,d));
}

alert(10.25.round(1)); // alerts 10.3
alert(10.25.toFixed(1)); // alerts 10.3
alert(typeof 10.25.round(1)); // alerts number
alert(typeof 10.25.toFixed(1)); // alerts string

</script>
Dr. Script
Copy linkTweet thisAlerts:
@anothenauthorSep 26.2004 — Aha! That's why I would get errors sometimes when I had to add variables. I discovered how to correct it by using Number(), but I didn't know why it was outputting stings. Some of my outputs would come out: ".. 10.321.65.7000 .." and so on.

Okay, I see you used a function in order to round your decimals. I'm gonna try something similar so I can just do a search and replace. I have this command dotted all over my code.

I found out about the Round command late last night. I had attempted to correct my code with the following:



// calculate quantity subtotal

// calculate track quantity price

f.track_count_price_temporary =

(f.track_count.value

* 10

*
track_width_price[f.stud_width.value]);

// f.track_count_price.value = f.track_count_price_temporary ;


temp_x100 = f.track_count_price_temporary * 100;

temp_mathround = Math.round(temp_x100);

temp_x001 = temp_mathround *
.01


f.track_count_price.value = temp_x001;


[/QUOTE]


It is crude but it works. But I think I can make the corrections by using a function, I'll work on that today.

Thank you for your help, it is greatly appreciated.

******** edit **********

I just discovered another problem. I can use the above to correct most of the errors, but now, some of my output will delete zeros. For example for $2.10 would output $2.1 , this may be a problem. Also I had an output that had zeros past my output box. I haven't figured that one out yet.

I'm thinking what I may do is separate the decimals from the number, round the decimals and use a different variable for the decimals and output both as whole numbers and then I can place my decimal in between.

<body><form> <ect>

<input NAME="track_count_rnd" size="4" OnFocus="document.wall_calculator.wall_length.select(); document.wall_calculator.wall_length.focus();">

.

<input NAME="track_count_dec" size="4" OnFocus="document.wall_calculator.wall_length.select(); document.wall_calculator.wall_length.focus();">

</body></form> </ect>
[/QUOTE]

I have a feeling I'm getting too extravagant. I think I may leave the code as is and when I learn some more PHP I'll update it then.

Most of my users will be contractors who have the latest browsers anyway.
Copy linkTweet thisAlerts:
@steelersfan88Sep 26.2004 — To show zeros, you'd need a string, because numbers will remove them. Otherwise, you may wnat to .toFixed() method after all, since the traling zeros aren't deleted.
×

Success!

Help @anothen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...