Welcome to WebmasterWorld Guest from 54.211.0.142

Forum Moderators: open

Message Too Old, No Replies

# JavaScript: formatted conversion of number to string

## Newbie to JavaScript

#### Mohamed_E

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

#### Senior Member

joined:May 14, 2002
posts:1192

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 :(

TIA!

#### txbakers

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

#### Senior Member

joined:Sept 1, 2001
posts:4392

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)

#### Preferred Member

joined:May 9, 2001
posts:416

...or the ternary solution:

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

All on one line:
result=(hour+":"+(minute<10?"0"+minute:minute));

T

#### Mohamed_E

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

#### Senior Member

joined:May 14, 2002
posts:1192

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.

#### txbakers

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

#### Senior Member

joined:Sept 1, 2001
posts:4392

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....

#### Mohamed_E

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

#### Senior Member

joined:May 14, 2002
posts:1192

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!

#### DrDoc

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

#### Senior Member

joined:Mar 15, 2002
posts:6807

confusing

...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...

#### DrDoc

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

#### Senior Member

joined:Mar 15, 2002
posts:6807

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)

#### Preferred Member

joined:May 9, 2001
posts:416

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.

T

#### HocusPocus

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

#### Junior Member

joined:Dec 27, 2002
posts:125

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

1 less "Leap".

=:-)

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

#### Preferred Member

joined:May 9, 2001
posts:416

Good job HP!

T

#### DrDoc

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

#### Senior Member

joined:Mar 15, 2002
posts:6807

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!

``[color=ffffff]......[/color]_[color=ffffff]...[/color]_``

``[color=ffffff]......[/color]\\//``

``,---¦¦---,``

``¦###¦¦###¦``

``'---''---'``

``,---,,---,``

``¦###¦¦###¦``

``'---''---'``

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

#### Preferred Member

joined:May 9, 2001
posts:416

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>