What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Jscript formatting numbers?

Jscript formatting numbers?
Author: Message:
Robin4286
Junior Member
**


Posts: 21
Joined: Sep 2006
O.P. Jscript formatting numbers?
okay, so say i have a variable named bob. Bob is equal to 1. Is there a way i can make it so that it displays bob as 01, sort of like toPrecision, but it adds zeroes before the number?
09-16-2006 01:22 AM
Profile E-Mail PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Jscript formatting numbers?
You need to create your own function for this as something like that doesn't exist in JScript (eg: like the Format() function in VB6). There are many ways to do it and what you will use mostly depends on your 'precision' and on your personal preferences.

Here are some of those ways:
(note: blue stuff is your precision; aka the stuff you need to change depending on how many digits you want)

Most common usage and method:
code:
var Bob = 3;
If (Bob < 10) Bob = "0" + Bob
though not really interesting if your precision is, let say, 7 digits. Because then you need something like:
code:
var Bob = 123;
if (Bob < 10) Bob = "0" + Bob;
if (Bob < 100) Bob = "0" + Bob;
if (Bob < 1000) Bob = "0" + Bob;
if (Bob < 10000) Bob = "0" + Bob;
if (Bob < 100000) Bob = "0" + Bob;
if (Bob < 1000000) Bob = "0" + Bob;
This can be shortened by using a loop (this also prevents the constant conversion that JScript needs to do between strings and numbers):
code:
var Bob = 123;
for (var i=String(Bob).length; i < 7; i++) Bob = "0" + Bob;
However, loops can be slow, especially when adding strings together. Though, in these cases I would assume you wouldn't want to add 1000 zero's in front of it so speed shouldn't be much of an issue.

However, another method which does not uses a loop would be something like:
code:
var Bob = 123;
if (String(Bob).length < 7) Bob = ("0000000" + Bob).substr(String(Bob).length);
But just as with the loop, you can have the problem that if Bob is lower than 100000, Bob will be a string and when it is higher than 999999 it will be a number. But in practice the difference between a number and a string shouldn't be noticeable in most cases though.

So you could disregard the next methods actually. But just for your information, here are the methods where Bob will always be converted to a string, even if there isn't a leading zero added:
code:
var Bob = 123;
Bob = (String(Bob).length < 7) ? ("0000000" + Bob).substr(String(Bob).length) : String(Bob);
This will always return a string, regardless of the length of Bob.
Yet another solution for this (with the loop):
code:
var Bob = 123;
Bob = String(Bob);
for (var i=Bob.length; i < 7; i++) Bob = "0" + Bob;
This will again always return a string, even if Bob has 7 or more digits.


--

So, all the above methods can be used even for numbers which have more digits than the amount of leading zeros you can add. If you explicitly know that your number will never be larger than the amount of digits you whish to represent, you can use even a very short 'trick' to add leading zeros:
code:
var Bob = 123;
Bob = String(Bob + Math.pow(10, 7)).substr(1);

This post was edited on 09-16-2006 at 03:36 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-16-2006 02:18 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On