Method of the Month: On.e();
Once a month I’d like to make it a habit to shed light on some of the methods we can find useful in our everyday coding lives. I don’t plan on drawing things out longer than they need to be so I’ll get right to showing you my monthly pick.
Language: JavaScript
Object: Number
I know what you’re thinking, “Too easy.” Well that’s ok. I wish someone told me about the various Number methods in JavaScript back when I had to code random projects at school in under 30min. Here is a nice simple look at four good methods for the Number object. We’ll go through them alphabetically.
(I’ll be using JavaScript’s constant for PI in the Math library for these examples)
number.toExponential(fractionDigits)
The toExponential method converts to a String of the number in its obvious exponential form. The optional fractionDigit is a parameter used to control the number of decimal places that are shown.
Math.PI.toExponential(); // 3.141592653589793e+0 Math.PI.toExponential(4); // 3.1415e+0 Math.PI.toExponential(0); // 3e+0
number.toFixed(fractionDigits)
Similar to the pervious method, toFixed converts the number to a String object. Our fractoinDigits still control the decimal place, but we aren’t having to deal with any extra baggage like an exponent suffix. Not entering a parameter into the method defaults to 0.
Math.PI.toFixed(); // 3 Math.PI.toFixed(0); // 3 Math.PI.toFixed(4); // 3.1415 Math.PI.toFixed(9); // 3.141592653
number.toPrecision(precision)
Ever need a number to be specific and not so robust? Most people would answer no, but this is still a nice tool if you need the number to be a certain length and you don’t mind exponential values or decimals in your data. The precision parameter controls the number of digits allowed.
(I’ll use a simple 123.45 number to prove a better point with this example)
var vNumber=123.45; vNumber.toPrecision(6); // 123.450 vNumber.toPrecision(4); // 123.5 vNumber.toPrecision(2); // 1.2e+2
number.toString(radix)
This one starts out simple then gets weird. The toString method is exactly what it sounds like. It converts the number object to a String. However this is when the radix parameter field is left blank or set to its default setting of 10, which represents a base of 10. Any other parameter converts the number to a String of itself as that base. We’ll go back to using Math.PI for this example.
Math.PI.toString(); // 3.141592653589793 Math.PI.toString(2); // 11.001001000011111101101010100010001000010110100011 Math.PI.toString(16); // 3.243f6a8885a3
Enjoy using some of these methods and I hope they help you out in some way or another.
DeMoor
July 14, 2010 in Computer Science, JavaScript, Method of the Month
Tagged as: JavaScript, Methods, Number

Pingback: Tweets that mention Methods of the Month: On.e(); « Adam DeMoor -- Topsy.com
Hey! Great article! I look forward to reading more from you on technical topics.
McGowan
Thanks Aaron. I hope to add more in regards to posts on computer science and the like.
it was very interesting to read.
I want to quote your post in my blog. It can?
And you et an account on Twitter?
lorky,
yeah I don’t mind, just add a link to this article and send me a link to your article through my contact page. Thanks. My twitter is @adamdemoor
Good post and this post helped me a lot in my college assignment. Thank you for your information.