How to add values in javascript when converting from stringHow do JavaScript closures work?How do I remove a property from a JavaScript object?Set a default parameter value for a JavaScript functionHow do I include a JavaScript file in another JavaScript file?JavaScript chop/slice/trim off last character in stringSort array of objects by string property valueHow to replace all occurrences of a string in JavaScriptConvert form data to JavaScript object with jQueryHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
Why don’t airliners have temporary liveries?
What happens when the attacking player dies to damage triggers after killing the blocking creatures in the first combat step of double strike?
Java guess the number
Translating 'Liber'
PL/SQL function to receive a number and return its binary format
What are the words for people who cause trouble believing they know better?
When writing an error prompt, should we end the sentence with a exclamation mark or a dot?
Does the "6 seconds per round" rule apply to speaking/roleplaying during combat situations?
Company did not petition for visa in a timely manner. Is asking me to work from overseas, but wants me to take a paycut
Is it recommended against to open-source the code of a webapp?
What LISP compilers and interpreters were available for 8-bit machines?
Why does Kathryn say this in 12 Monkeys?
How to make thick Asian sauces?
Why does this sentence use 东西?
Is it possible for people to live in the eye of a permanent hypercane?
Can a user sell my software (MIT license) without modification?
How to make a setting relevant?
Turing patterns
Approximate solutions to non polynomial equations
Can characters escape from Death House through this method?
Does Lightning Network has concept of continuous stream of value?
Why don't B747s start takeoffs with full throttle?
Will TSA allow me to carry a Continuous Positive Airway Pressure (CPAP)/sleep apnea device?
Movie about a boy who was born old and grew young
How to add values in javascript when converting from string
How do JavaScript closures work?How do I remove a property from a JavaScript object?Set a default parameter value for a JavaScript functionHow do I include a JavaScript file in another JavaScript file?JavaScript chop/slice/trim off last character in stringSort array of objects by string property valueHow to replace all occurrences of a string in JavaScriptConvert form data to JavaScript object with jQueryHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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);
javascript double decimal
add a comment |
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);
javascript double decimal
add a comment |
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);
javascript double decimal
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);
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);
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);
javascript double decimal
javascript double decimal
edited Mar 24 at 16:42
Code Maniac
15.1k21035
15.1k21035
asked Mar 24 at 15:19
AndreasAndreas
1881111
1881111
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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)
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
Mar 24 at 15:40
@Andreas it will use only 7 decimals as it was doing previously.
– Code Maniac
Mar 24 at 15:41
@Code Maniac I see, so it is not possible to caluculate with 16 decimals at all in javascript/node.js?
– Andreas
Mar 24 at 15:45
add a comment |
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
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.
– Andreas
Mar 24 at 15:55
Thank you for your help. It was helpful!
– Andreas
Mar 24 at 15:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325288%2fhow-to-add-values-in-javascript-when-converting-from-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
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
Mar 24 at 15:40
@Andreas it will use only 7 decimals as it was doing previously.
– Code Maniac
Mar 24 at 15:41
@Code Maniac I see, so it is not possible to caluculate with 16 decimals at all in javascript/node.js?
– Andreas
Mar 24 at 15:45
add a comment |
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)
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
Mar 24 at 15:40
@Andreas it will use only 7 decimals as it was doing previously.
– Code Maniac
Mar 24 at 15:41
@Code Maniac I see, so it is not possible to caluculate with 16 decimals at all in javascript/node.js?
– Andreas
Mar 24 at 15:45
add a comment |
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)
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)
console.log(typeof (1).toFixed(2))
console.log(typeof (1).toFixed(2))
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)
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)
answered Mar 24 at 15:35
Code ManiacCode Maniac
15.1k21035
15.1k21035
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
Mar 24 at 15:40
@Andreas it will use only 7 decimals as it was doing previously.
– Code Maniac
Mar 24 at 15:41
@Code Maniac I see, so it is not possible to caluculate with 16 decimals at all in javascript/node.js?
– Andreas
Mar 24 at 15:45
add a comment |
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
Mar 24 at 15:40
@Andreas it will use only 7 decimals as it was doing previously.
– Code Maniac
Mar 24 at 15:41
@Code Maniac I see, so it is not possible to caluculate with 16 decimals at all in javascript/node.js?
– Andreas
Mar 24 at 15:45
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
Mar 24 at 15:40
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
Mar 24 at 15:40
@Andreas it will use only 7 decimals as it was doing previously.
– Code Maniac
Mar 24 at 15:41
@Andreas it will use only 7 decimals as it was doing previously.
– Code Maniac
Mar 24 at 15:41
@Code Maniac I see, so it is not possible to caluculate with 16 decimals at all in javascript/node.js?
– Andreas
Mar 24 at 15:45
@Code Maniac I see, so it is not possible to caluculate with 16 decimals at all in javascript/node.js?
– Andreas
Mar 24 at 15:45
add a comment |
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
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.
– Andreas
Mar 24 at 15:55
Thank you for your help. It was helpful!
– Andreas
Mar 24 at 15:59
add a comment |
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
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.
– Andreas
Mar 24 at 15:55
Thank you for your help. It was helpful!
– Andreas
Mar 24 at 15:59
add a comment |
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
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
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))
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))
const theBiggestInt = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
// ↪ 9007199254740991n
const hugeString = BigInt("9007199254740991");
// ↪ 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff");
// ↪ 9007199254740991n
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
// ↪ 9007199254740991n
const theBiggestInt = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
// ↪ 9007199254740991n
const hugeString = BigInt("9007199254740991");
// ↪ 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff");
// ↪ 9007199254740991n
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
// ↪ 9007199254740991n
edited Mar 24 at 15:49
answered Mar 24 at 15:27
RaymondRaymond
1,115415
1,115415
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.
– Andreas
Mar 24 at 15:55
Thank you for your help. It was helpful!
– Andreas
Mar 24 at 15:59
add a 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.
– Andreas
Mar 24 at 15:55
Thank you for your help. It was helpful!
– Andreas
Mar 24 at 15:59
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.
– Andreas
Mar 24 at 15:55
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.
– Andreas
Mar 24 at 15:55
Thank you for your help. It was helpful!
– Andreas
Mar 24 at 15:59
Thank you for your help. It was helpful!
– Andreas
Mar 24 at 15:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325288%2fhow-to-add-values-in-javascript-when-converting-from-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown