search

Home  >  Q&A  >  body text

How to convert a string to a 19-digit number in javaScript without automatic rounding?

We have string input numbers input="6145390195186705543" If we want to convert to a number; the java script automatically rounds this input;

(6145390195186705543)

(6145390195186705000)

P粉652495194P粉652495194267 days ago557

reply all(1)I'll reply

  • P粉025632437

    P粉0256324372024-03-31 12:23:41

    This should suit what you want to achieve

    const myFunction = (num) => {
    
    const length = num.length;
    
    const result = `(${num.slice(0,16).padEnd(length, "0")})`;
    return result;
    };
    
    const input="6145390195186705543";
    const output = myFunction(input); 
    console.log(output)

    reply
    0
  • Cancelreply