1

I'd like to input numbers into a text field and have them pass through the decimal place. I've seen this question - How to move decimal? - and already had code very similar to it. But, I'd like it to read

$00.00 after inputing a 1,

$00.01, another 1:

$00.11, a 2:

$01.12, a 231:

$1122.31. But I don't want the decimal to move- it should stay in the same place. Is there a name for this? Microwave input?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Jared
  • 2,713
  • 10
  • 31
  • 59

1 Answers1

1

So it's easy man, divide it by 100:

function my(n)
{
    var l = n.toString().length;
    var r = n/100;
    //document.write(r); for debugging purpose

    return r;
}

I'll add formatting..

Update:

function mynum(n)
{
    var l = n.toString().length;
    var r = n/100;
    var padded = pad(r, 5, '0'); 
    // I use 5 symbols here because of dot '.' in the number

    return '$' + padded;    
}

function pad(n, width, z) 
{
    z = z || '0';
    n = n + '';
    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

For pad I used this: https://stackoverflow.com/a/10073788/3172092

Community
  • 1
  • 1
George Garchagudashvili
  • 6,657
  • 12
  • 39
  • 53
  • [why document.write is bad practise](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice). – Regent Oct 02 '14 at 06:27
  • it was just testing purpose :) – George Garchagudashvili Oct 02 '14 at 06:28
  • This looks like it's great, but is there a way to ensure there are at a minimum of 4 figures? So if I ran `my(1)` now it would return `.01` and I would like `00.01`? I know how to ensure figures on the right of a decimal (more precision) but not on the left. – Jared Oct 02 '14 at 06:31
  • The problem is in fact, that you added it in _testing purpose_, but people will use it in their code without any doubts. And when someone asks them why do they use this bad thing, they will say: "George Garchagudashvili told us to do it this way" :) – Regent Oct 02 '14 at 06:37
  • I we did not use it anytime newbies will never know what it is so :)) Also I assume he has enough knowledge in Javascript, to mention it himself ;)) But I'll comment it don't worry – George Garchagudashvili Oct 02 '14 at 06:39