homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

JavaScript: formatted conversion of number to string
Newbie to JavaScript

 4:54 pm on Apr 17, 2003 (gmt 0)

I am building a calculator which computes time to accomplish a task. Internally I get a time in floating point, I want to output it as hour:minute. OK, no problem splitting the time into hour and fraction, again no problem converting the fraction to integer minutes. I then end the function with:

result = hour + ":" + minute
return result

Which works fine if (minute >= 10). But how do I get a leading zero if (minute < 10)?

In case I am not totally clear let me give an example. For 3.25 hours I get 3:15, while for 3.1 hours I get 3:6 instead of 3:06 :(




 5:56 pm on Apr 17, 2003 (gmt 0)

Not a big problem. Before you concatenate the final string, check the value of minute, and if less than 10, concatenate a "0" to the front of it.

You might have to formally declare your variable as a string first, so it doesn't just add the two integers together. You might have to use a "toString()" method, but try the below first.

I think this will work:

if (minute < 10) {
minute = "0" + minute;

result = hours + ":" + minute;


 5:59 pm on Apr 17, 2003 (gmt 0)

...or the ternary solution:

var min = (minute<10?"0"+minute:minute);

All on one line:



 6:32 pm on Apr 17, 2003 (gmt 0)

Many thanks to both of you!

As a long time (more accurately, long time ago) C programmer I love the ternary form, will use it.


 7:06 pm on Apr 17, 2003 (gmt 0)

I love the ternary thing too. I don't use it as often as I should because at 3 AM it's confusing to understand....


 7:23 pm on Apr 17, 2003 (gmt 0)

While you both solved my immediate problem, I am curious about the general question: Does JavaScript have a formatted number to string conversion, like the sprintf() of C and similar languages? I searched w3schools and did not find anything, but the information there is pretty basic (enough to write my calculator, though).

This was my first JavaScript program, and though I do not see myself using it much, you never know!


 7:25 pm on Apr 17, 2003 (gmt 0)


...like when you need to nest them four or five levels ;) Like when I had to make my own leap year check. Try expressing this in one line, using ternary functions:

"The year is a leap year if modulus 4 is zero, unless modulus 100 is also zero, unless modulus 400 is zero as well" :)

Hence, 1900 is not leap year, 1976 is, and so is 2000...


 7:32 pm on Apr 17, 2003 (gmt 0)

JavaScript is more forgiving than C ;)

a = 4; // a is now an integer
a += ""; // a is now a string
a = Math.round(a); //a is now an integer again

There are functions, but basically... if you're using a string function, it will become a string. If you're using an int function, it will become an int.

Now, certain care has to be taken. This would not work!

a = 400;
b = substring(a,0);

However, this would:

a = 400;
b = substring(a+"",0);


 11:59 pm on Apr 17, 2003 (gmt 0)

Sorry for spewing code and disappearing Mohamed_E. I took a nap.

As DrDoc points out Javascript is a heavily untyped language, with generous automatic value conversion.

Generally speaking, any value used in the context of another will convert to fit the context. Hence "0"+minute (integer concatenated to a string) results in a string.


I've tested this a bit, but I don't have a long list of leap years and that's the damnedest thing to find on the web!?

var year = 1201;
document.write(year%4==0&&year%100!=0?"Leap":(year%400!=0?"No Leap":"Leap"));

If it performs to your satisfaction may I have a prize please. I'd like a prize. I need a prize.



 12:56 am on Apr 18, 2003 (gmt 0)

(year%4!= 0 year%100 == 0 && year%400!= 0?"No Leap":"Leap")

1 less "Leap".



 1:16 am on Apr 18, 2003 (gmt 0)

Good job HP!



 3:17 am on Apr 18, 2003 (gmt 0)

Yes, well done both of you :)

I don't remember exactly what my function looked like, but HP's thingy looks familiar...

Oh, prize... Here you go!



 3:46 am on Apr 18, 2003 (gmt 0)

Thankyou. (oh, it's heavy, hehe)

It's been a long road from humble beginnings in Ecclefechen to this, the night of nights in the greatest shrine of web programming arts - it is indeed an honour - and there are more than a few dedicated people I must mention, without whose help... <yank>

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved