Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to round the factorial value in java scripts if we have below value
7.156945704626378 e +118
I want 7.16 only
please let me know if we have any solution?
var number=7.1569;
number*=100;
var new_number = Math.round(number);
new_number/=100;
((Number(7.156945704626378e+118))/1.0e+118).toFixed(2);
try this
you can refer links
Formatting a number with exactly two decimals in JavaScript
and
How to convert a String containing Scientific Notation to correct Javascript number format
Put it in a String variable:
var x = "7.156945704626378e+118";
Then use a regular expresion which checks for the part before e and parse it back to float:
var y = parseFloat(x.match(/\d+[.][0-9]+/)[0]);
And round to 2 decimal points by:
var z = y.toFixed(2);
If you still want your e118 just multiply z with 1e118 then.
Here is a working fiddle
I am not sure whether this approach is correct but this give what user want in question.
Number((+num.toString().replace(/e.*/, '')).toFixed(2))
DEMO
var num = 7.156945704626378e+118;
console.log(Number((+num.toString().replace(/e.*/, '')).toFixed(2)));
# 7.16
Note:
This assumes that exponent is not significant and mantissa is what all required for rounding off.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
const a = 043
console.log(Number(a))
Here the variable a is octal because of which we get the result as 35.
Instead, I want the variable a to be a number 43.
Found results for removing leading zeros from a string(Remove leading zeros from a number in Javascript)
Because your value starts with 0, and its type is number instead of string, it will be recognized as octal, and when you use it directly, javascript will convert it to decimal
The fastest way you can use Number.prototype.toString(radix)
let a = 043
console.log(a.toString(8))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to check if a number has a 2 decimal place precision. I know how to convert a number to a 2 decimal place precision like this:
var myNumber = 2.456;
var currentNumber = parseFloat(myNumber);
currentNumber = currentNumber.toFixed(2);
console.log(currentNumber);
so If I enter:
1 this will turn into 1.00
2.457 this will turn into 2.46
but how can I check if the result of my code has a 2 decimal place precision? Thanks a lot in advance!
Parse the number into a string, split by a period and check whether the second item's length is 2:
function isPrecise(num){
return String(num).split(".")[1]?.length == 2;
}
console.log(isPrecise(1.23))
console.log(isPrecise(1.2))
console.log(isPrecise(1))
console.log(isPrecise("1.23")) //works if its a string too
You can also use a regex:
function isPrecise(num){
return /\d+\.\d{2}/gm.test(String(num));
}
console.log(isPrecise(1.23))
console.log(isPrecise(1.2))
console.log(isPrecise("1.23")) //works if its a string too
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am wondering how to remove decimals in large numbers without it having to round off?
My float is:
number = 32.223516
The result that I want is 32223516.
When I do this:
parseInt(number * 1000000, 10) it gives me 32223515.
And if I use Math.ceil or Math.round, it'll be a problem for cases that would need it to round down.
Is there a cleaner way to do this?
Advance thanks!
Number.parseInt(
yourVariable.toString().replace('.', '')
);
Try this:
let number = 32.223516
console.log(parseInt(number.toString().replace('.', '')));
If you know that the number always needs to be multiplied by 1000000 to get what should be the whole number version of it, do that with Math.round:
console.log(Math.round(32.223516 * 1000000));
Otherwise, I'd be tempted to take a round trip through a string:
console.log(+(32.223516).toString().replace(".", ""));
...but you need to beware of scientific notation, see the answers here for how to convert the number to string without scientific notation.
That is an example of how JavaScript handles all numbers as floating-point numbers.
What can help you in this case is if you convert the number to string form, retain only the desired amount of numbers after the decimal point, which is probably 0 in your case, by using toFixed() method, which would convert and round-off accordingly, like so:
number = 32.223513;
numberToFixed = (num * 1000000).toFixed(0); //argument is amount of numbers to be retained after decimal point
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
let num = 100;
document.write("Num: " + num + "");
document.write("Binary: " + num.toString(2) + "");
document.write("8: " + num.toString(8) + "");
document.write("16: " + num.toString(16));
//I want this, but without toString() or any other method.
welcome to Stack Overflow! This sounds like a homework question, because you have been asked to implement something that would normally be done using builtin methods. Here is the outline of how I would approach this, to help you get started.
If you had to write the number 100 in base 16, what would the last digit of the answer be?
How did you come to that conclusion?
Now what would the decimal value of the remainder of the hexadecimal number have to be? Hint: answer = 96.
How did you come to that conclusion?
Your last answer will be a multiple of 16, because you have removed already the remainder after dividing by 16. You have done: number - (number modulo 16).
If you divide this by 16, you have a new, smaller number, which again you can convert into hexadecimal in the same way as above. In this manner you can keep extracting one hexadecimal digit at a time, until there is no reminder.
This gives your hexadecimal number, outputted in reverse order.
You can do the same for any base.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have tried with value.tofixed(7).
When I'm giving int value(3) its returning decimal (7) (e.g) 3.0000000.
At the same time if I give a float value(3.3) its returning decimal(6) (e.g) 3.300000
How to solve this?
3.300000 has 6 decimal places because of the code rounded one 0 to place the 3.
try this:
var value = 3;
var n = value.tofixed(7);
and
var value2 = 3.30;
var n = value2.tofixed(7);
I think you need to use :
- Math.floor() : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Math/floor.
- or Math.ceil() : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Math/ceil
Instead of toFixed().
There is another method called Math.round() which makes the round (upper or lower depending of the number you round) automatically without asking you about the floor/ceil param'.
You can use toFixed() which return a fixed number of decimal point value (rounded). You can also use value.toPrecision(x) which will return x number of total digits.