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. =?Utf-8?B?dHJlbGxvdzQyMg==?=

    HELP! HELP! HELP! Opening Web Application Project Error

    =?Utf-8?B?dHJlbGxvdzQyMg==?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,008
    =?Utf-8?B?dHJlbGxvdzQyMg==?=
    Feb 20, 2004
  2. Harvey
    Replies:
    0
    Views:
    1,098
    Harvey
    Jul 16, 2004
  3. Harvey
    Replies:
    1
    Views:
    1,175
    Daniel
    Jul 16, 2004
  4. =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=

    HELP - HELP - HELP

    =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=, Mar 9, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,006
    Eliyahu Goldin
    Mar 9, 2006
  5. Buster

    Help, Help, Help

    Buster, Oct 4, 2003, in forum: Java
    Replies:
    3
    Views:
    718
    Saager
    Oct 30, 2003
  6. Usman

    HELP HELP HELP

    Usman, Jan 22, 2004, in forum: C++
    Replies:
    7
    Views:
    515
    Usman
    Jan 23, 2004
  7. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    887
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  8. francisco lopez
    Replies:
    2
    Views:
    443
    Hywel Jenkins
    Dec 30, 2004
Loading...