Closed Thread Icon

Topic awaiting preservation: Format number (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=24472" title="Pages that link to Topic awaiting preservation: Format number (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Format number <span class="small">(Page 1 of 1)</span>\

 
SmashHallon
Nervous Wreck (II) Inmate

From: Sweden
Insane since: Mar 2003

posted posted 12-22-2004 16:55

Hi,

Does anyone have a function for converting a number to "time-format"?

Ex. If I type 8 and tab out of a field I would like it converted to 08,00

Same with:
1,30 = 01,30
01,3 = 01,30
1200 = 12,30 and so on

Just wonder if you have it, i have tried to create it but the code gets to messy!

Kind regards

InI
Maniac (V) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 12-22-2004 17:46

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 12-22-2004 20:56

" 1200 = 12,30 "

For the others, it's just a question of adding some zeros and cutting the string at the right position.

If there's no coma, you can simply add ",00" to the string. Otherwise, add "00" at the end and cut the string 2 characters after the coma. If the coma is not the 3rd character, add 2 minus position of the coma "0" at the begining of the string and voilà.



(Edited by poi on 12-22-2004 20:57)

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-22-2004 21:41

comma not coma



.:[ Never resist a perfect moment ]:.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 12-22-2004 21:44



Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 12-23-2004 01:27

Yeah... it's just a matter of converting the number to a string and padding a zero if it's shorter than you want... should be pretty simple. :X

var foo = 5;
var bar = foo.toString(10);
while(bar.length < 2) bar = "0" + bar;

alert(bar);

(untested but should work :X)

"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

SmashHallon
Nervous Wreck (II) Inmate

From: Sweden
Insane since: Mar 2003

posted posted 12-24-2004 19:02

Hehe, I am so sorry...

I have actually googled it. I am called Mr Google by some friends. But this time I did not find anything useful in my opinion. Maybe I didnt Google enough. Fair enough.

I did not ask anyone to do it for me, just asked if someone already have done a smart function for this and was kind to share it. So that I didn´t have to invent the wheel again, hehe.

I have and can of course make this function with lots of if-statements...

If indexOf string has a comma and the length of the string is 4 and bla bla then insert 0 before and so on, but I just wonder if there is some chance of making this with regexps or some kind of built in padding javascript functions that I cant find on google.

If I would ask you guys stuff every time I have a problem this asylum would be spammed

poi: yes 12 = 12.30 was a typo, sorry!

Merry Christmas by the way!

(Edited by SmashHallon on 12-24-2004 19:06)

« BackwardsOnwards »

Show Forum Drop Down Menu