Convert Large Decimal Number to String in Javascript [duplicate] - javascript

This question already has answers here:
How to deal with big numbers in javascript [duplicate]
(3 answers)
Closed 5 years ago.
I have large decimal numbers which I am getting from a request & I want to convert them to string.
So for EG:
I tried all methods converting to string
var r=12311241412412.1241523523523235
r.toString();
r+'';
''+r;
String(r);
//output
'12311241412412.1241'
//what i want
'12311241412412.1241523523523235'
All methods return the decimal numbers upto 4 digits (12311241412412.1241)
but i want all the number till end.
I also tried r.toFixed().toString() but each time the length of decimal numbers change.
What would be easy way to do this?

the problem is that 12311241412412.1241523523523235 in javascript means 12311241412412.125. whatever you do is not gonna work unless you put the whole thing in a string at the first place.
use this instead:
var r = "12311241412412.1241523523523235";

Related

How can I make this js code output to one decimal place? How do implement in MY SPECIFIC case? [duplicate]

This question already has answers here:
How do you round to 1 decimal place in Javascript?
(24 answers)
Closed 4 years ago.
This code outputs a number with many decimal places.. I'd like it limited to just one... this code gets me my desired result BUT with too many characters displayed.
I'm a total noob....
function oninputchange() {
$output.textContent = $input.value.split(/\s/g).map(syllable).reduce(sum)/4.7;
}
The results are expected... Just want fewer decimal places
Use method toFixed() for limit the decimal number to show

Why does it show value 5150 and not 155? [duplicate]

This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 4 years ago.
So im quite new to javascript and i tried making something simple as entering a value and add 150 to it but it wont show number + 150?
Pictures below
Code
Output
That's because you are concatenating strings. You need to convert the strings to integers by using parseInt(), and then add the numbers.
https://www.w3schools.com/jsref/jsref_parseint.asp
var fullprice = parseInt(price) + 150;
Because you're actually concatenating a string with a number. The input value is a string, so before operating with it, parse it to int with parseInt
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

jQuery not returning number from an HTML tag [duplicate]

This question already has answers here:
Javascript parse float is ignoring the decimals after my comma
(10 answers)
Closed 7 years ago.
Ok so I'm having a bit of trouble returning a number from an html tag using jquery.
So lets say i have this: <p class="number">4,500.50</p> and i want to get the number from this tag using jquery, so I have the following.
var number = parseFloat($('.number').html());
But this only returns the number 4 instead of the full number. I also treied with the .text() method but the result is the same. Any ideas as to how to resolve this? Any help is appreciated.
Example jsfiddle: https://jsfiddle.net/zf1ctums/1/
Your input string has a non standard (well probably in certain contries people are used to it) format. parseFloat only knows about digits and decimal POINT. So you need to delete the commas:
var number = parseFloat($('.number').html().replace(',', ''));
The problem is that the number should be 4500,50.
You should not pass the thousand separator.
Take a look here.

Javascript very simle function returns weird values [duplicate]

This question already has answers here:
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 7 years ago.
I have the folowing code:
Click
function Add(id) {
alert(id);
}
The value in the alert is 23905762501722144 (-2) from the original value.
Why does this happen?
https://jsfiddle.net/wvtqostd/4/
log2(23905762501722146) ~= 54.408
JavaScript stores all numbers - including integers - as double precision floats. Double precision mantissa/significand contains 52 bits of information, so some information gets lost storing so long/precise number as you have.
Because 23905762501722144 is to big to represent as integer value... try sending it as string value.

(Js) number method to add a comma after the first number and remove the 2 number after comma ?? is there any? [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);

Categories