0

I have a string that I try to convert to a decimal number with 16 decimals. Then I like to add that number to the number 0 which should be:

30280.9529335

But I get: 030280.9529335

How do you properly do this in javascript?

var totalnumber = 0; var str = "30280.9529335";
totalnumber = totalnumber + ConvertToDouble(str);

console.log(totalnumber); //030280.9529335

function ConvertToDouble(x) {
    return Number.parseFloat(x).toFixed(16);
}

2 Answers 2

1

Well your problem is placement of toFixed, toFixed return String not number

console.log(typeof (1).toFixed(2))

So here your ConvertToDouble function returns string and 0 + some numeric string will act as concatenation not addition

var totalnumber = 0; var str = "30280.9529335";
totalnumber = totalnumber + ConvertToDouble(str);

console.log(totalnumber.toFixed(16)); //030280.9529335

function ConvertToDouble(x) {
    return Number.parseFloat(x)
}

Sign up to request clarification or add additional context in comments.

3 Comments

It worked but I wonder: If I remove ".toFixed(16)", does it still use 16 decimals when calculating or is it Float with only 7 decimals?
@Andreas it will use only 7 decimals as it was doing previously.
@Code Maniac I see, so it is not possible to caluculate with 16 decimals at all in javascript/node.js?
0

You can use Number.parseFloat and Number.parseInt similar to how you were. You used toFixed incorrectly.

The toFixed() method converts a number into a string, keeping a specified number of decimals. If the desired number of decimals are higher than the actual number, nulls are added to create the desired decimal length.

Example:

let a = "30280.9529335";
console.log(parseFloat(a))
// 30280.9529335

let totalnumber = 0;
let str = "30280.9529335";

function convert(a, b) {
    try {
        return (Number.parseInt(a) + Number.parseFloat(b)).toFixed(16)
    } catch(error) {
        return error;
    }
}

function convertb(a, b) {
    try {
        return Number.parseInt(a) + Number.parseFloat(b)
    } catch(error) {
        return error;
    }
}

   console.log(convert(totalnumber, str))
   console.log(convertb(totalnumber, str))

You can also use bigInts https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

const theBiggestInt = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
// ↪ 9007199254740991n
const hugeString = BigInt("9007199254740991");
// ↪ 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff");
// ↪ 9007199254740991n
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
// ↪ 9007199254740991n

1 Comment

I was not sure about the .toFixed(16), I thought it returned a decimal number with 16 places. So it was a string. That makes sense to the concatenation that happened.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.