Help with javascript

Discussion in 'ASP .Net' started by Morten Snedker, Dec 1, 2006.

  1. I'm fairly new to aspnet, actually I'm on my very first project. I've
    worked with vb.net, vb and vba through some years, but with aspnet
    I've been forced into something new: javascript.

    I'm all new to javascript and need some help with the script below. It
    is supposed to return a number in Danish format, which would be
    something like
    10.500,00
    that is . as 1000-separator and , before the two decimals.

    However, if I enter 1000,50 it returns
    100.050,00

    I can't figure out where the fault is. Can someone help me?


    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+'.'+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + ',' + cents);


    Regards /Snedker
     
    Morten Snedker, Dec 1, 2006
    #1
    1. Advertisements

  2. Morten Snedker

    shekhargood Guest

    first u must parse that string to int with var
    roundedValue=parseInt(stringname);then use math.round(roundedValue);
    thats it


    On Dec 1, 1:03 pm, Morten Snedker <morten_spammenot_ATdbconsult.dk>
    wrote:
    > I'm fairly new to aspnet, actually I'm on my very first project. I've
    > worked with vb.net, vb and vba through some years, but with aspnet
    > I've been forced into something new: javascript.
    >
    > I'm all new to javascript and need some help with the script below. It
    > is supposed to return a number in Danish format, which would be
    > something like
    > 10.500,00
    > that is . as 1000-separator and , before the two decimals.
    >
    > However, if I enter 1000,50 it returns
    > 100.050,00
    >
    > I can't figure out where the fault is. Can someone help me?
    >
    > num = num.toString().replace(/\$|\,/g,'');
    > if(isNaN(num)) num = "0";
    > sign = (num == (num = Math.abs(num)));
    > num = Math.floor(num*100+0.50000000001);
    > cents = num%100;
    > num = Math.floor(num/100).toString();
    > if(cents<10)
    > cents = "0" + cents;
    > for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    > num = num.substring(0,num.length-(4*i+3))+'.'+
    > num.substring(num.length-(4*i+3));
    > return (((sign)?'':'-') + num + ',' + cents);
    >
    > Regards /Snedker
     
    shekhargood, Dec 1, 2006
    #2
    1. Advertisements

  3. Morten Snedker

    shekhargood Guest

    first parse that string to int by using var int=parseInt(string); then
    use math.round(int); thats it
     
    shekhargood, Dec 1, 2006
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mcdeveloper
    Replies:
    1
    Views:
    4,424
    mcdeveloper
    Jun 13, 2006
  2. CRON
    Replies:
    25
    Views:
    210,724
    davidb
    May 29, 2015
  3. Mark Rae

    JavaScript or not JavaScript

    Mark Rae, Sep 5, 2006, in forum: ASP .Net
    Replies:
    36
    Views:
    1,265
    Paul Sture
    Sep 9, 2006
  4. Nathan Sokalski
    Replies:
    4
    Views:
    730
    PJ on Development
    Nov 8, 2007
  5. francisco lopez
    Replies:
    2
    Views:
    363
    Hywel Jenkins
    Dec 30, 2004
Loading...

Share This Page