Given this code:
const value = 1;
Math.sin(2 * Math.PI * value).toFixed(5);
Why does this return "-0.00000", when the value before .toFixed(5) is -2.4492935982947064e-16?
The number is in scientific notation.
The e-16 means that there are 16 0's to the left of the number.
-2.4492935982947064e-16
is really
-0.00000000000000024492935982947064
When you run toFixed(5), you end up with 5 decimal places, which are all 0's.
The number you presented -2.4492935982947064e-16 is in scientific notation. That number would be equivalent to -2.4492935982947064 × 10^-16 which would be exactly -0.00000000000000024492935982947064 after expanding it.
-2.4492935982947064e-16 is -2.4492935982947064 * Math.pow(10,-16), so 5 places after decimal point is not enough to see something different than 0
const value = 1;
let result = Math.sin(2 * Math.PI * value);
console.log(result)
console.log(result.toFixed(20))
console.log(result.toFixed(5))
Related
I'm trying to get a number to the nth decimal place without rounding off
The closest I could find was from this source
num = num.toString(); //If it's not already a String
num = num.slice(0, (num.indexOf("."))+3); //With 3 exposing the hundredths place
Number(num); //If you need it back as a Number
but it has it's limitations
this is what I'm trying to achieve:
if n=3
16 -> 16.000
16.000001 -> 16.000
16.12345 -> 16.123
4239.20902190 -> 4239.209
I'm trying to stay way from a mathematical approach and rather use a string approach as mathematical approaches sometimes become unpredictable, so is there any modern way of achieving the desired results?
I may have put an incorrect title to the problem so any edit is welcome
If you multiply a number by 10, then use Math.floor to remove everything after the decimal place, THEN divide by 10, you get the value of the original number to one decimal place with no rounding. If instead of 10 you use 100, it'll be 2 decimal places. 1000 makes 3, 10000-4, etc.
Then using number.prototype.ToFixed(n), we can get a string out that will always have n decimal places.
Combining these together you get something like:
function toDecimalPlaceWithoutRounding(number, precision) {
const factor = 10 ** precision; // "x ** y" means x to the power of y
const value = Math.floor(number * factor) / factor;
return value.toFixed(precision);
}
a quick test of this:
function toDecimalPlaceWithoutRounding(number, precision) {
const factor = 10 ** precision;
const value = Math.floor(number * factor) / factor;
return value.toFixed(precision);
}
for (let i = 0; i < 10; i++) {
const number = Math.random() * 20;
const result = toDecimalPlaceWithoutRounding(number, 3);
console.log(number,result);
}
NOTE you could just use .toFixed, but it will round. eg. (3.555).toFixed(2) will give "3.56".
EDIT negative support:
function toDecimalPlaceWithoutRounding(number, precision) {
const factor = 10 ** precision;
const roundFunc = number > 0 ? Math.floor : Math.ceil; // use floor for positive numbers, ceil for negative
const value = roundFunc(number * factor) / factor;
return value.toFixed(precision);
}
for (let i = 0; i < 10; i++) {
const number = Math.random() * 20 - 10;
const result = toDecimalPlaceWithoutRounding(number, 3);
console.log(number,result);
}
So I found the solution thanks to #Callum Morrisson
by using Math.trunc():
function toPrecision(num,precision)
{
num=Math.trunc(num*10**precision)/10**precision;
return num;
}
console.log(toPrecision(-21873212130.119281231231231,4))
jsfiddle here
This can be achieved by 10**n and Math.round()
function roundWithPrecision(num,n){
//return Math.round(num * 10 ** n)/(10**n)
return Math.floor(num * 10 ** n)/(10**n)
}
A test of the function:
a=roundWithPrecision(16.12345,4)
16.1234
b=roundWithPrecision(16.123456,5)
16.12346
Hmm I have an issue with roundings on the client side which is then validated in the backend and the validation is failing due to this issue. Here is the previous question Javascript and C# rounding hell
So what I am doing is:
On client side:
I have 2 numbers: 50 and 2.3659
I multiply them: 50 * 2.3659 //118.29499999999999
Round to 2 decimal places: kendo.toString(50 * 2.3659, 'n2') //118.29
In backend(C#):
I am doing the same: 50 and 2.3659
I multiply them: 50 * 2.3659 //118.2950
Round to 2 decimal places: Math.Round(50 * 2.3659, 2) //118.30
And validation is failing. Can I do something on the client side?
Can you try parseFloat and toFixed functions as follows :
var mulVal = parseFloat(50) * parseFloat(2.3659);
var ans = mulVal.toFixed(2);
console.log(ans);
Javascript Arithmetic is not always accurate, and such erroneous answers are not unusual. I would suggest that you use Math.Round() or var.toFixed(1) for this scenario.
Using Math.Round:
var value = parseFloat(50) * parseFloat(2.3659);
var rounded = Math.round(value);
console.log(rounded);
Prints 118 to the console.
Using toFixed() method:
var value = parseFloat(50) * parseFloat(2.3659);
var rounded = value.toFixed(1);
console.log(rounded);
Prints 118.3 to the console.
Note that using toFixed(2) will give the value as 118.29.
Hope this helps!
Haven't tested this extensively, but the function below should emulate the 'MidPointToEven' rounding:
function roundMidPointToEven(d, f){
f = Math.pow(10, f || 0); // f = decimals, use 0 as default
let val = d * f, r = Math.round(val);
if(r & 1 == 1 && Math.sign(r) * (Math.round(val * 10) % 10) === 5)
r += val > r ? 1 : -1; //only if the rounded value is odd and the next rounded decimal would be 5: alter the outcome to the nearest even number
return r / f;
}
for(let d of [50 * 2.3659, 2.155,2.145, -2.155, 2.144444, 2.1, 2.5])
console.log(d, ' -> ', roundMidPointToEven(d, 2)); //test values correspond with outcome of rounding decimals in C#
I have float numbers:
var a = parseFloat("12.999");
var b = parseFloat("14");
And I want to display them as:
12.99
14.00 -> with zeros
But without round, only truncate. How to do it?
You use a combination of the Math.floor() and Number.prototype.toFixed() function, like this:
console.log((Math.floor(a * 100) * 0.01).toFixed(2));
console.log((Math.floor(b * 100) * 0.01).toFixed(2));
Math.floor() will truncate the value to the closest lower integer. That is why you need to first multiply by 100 and then multiply by 0.01.
Number.prototype.toFixed() will format your output using a set number of decimals.
Most languages do have functions called round, ceil, floor or similar ones, but almost all of them round to the nearest integer, so the multiply-round-divide chain (or divide-round-multiply for rounding to tens, hundreds, thousands...) is a good pattern to know.
You can first truncat the part, you do not need.
function c(x, p) {
return ((x * Math.pow(10, p) | 0) / Math.pow(10, p)).toFixed(p);
}
document.write(c(12.999, 2) + '<br>');
document.write(c(14, 2));
This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 5 years ago.
I have the following JavaScript syntax:
var discount = Math.round(100 - (price / listprice) * 100);
This rounds up to the whole number. How can I return the result with two decimal places?
NOTE - See Edit 4 if 3 digit precision is important
var discount = (price / listprice).toFixed(2);
toFixed will round up or down for you depending on the values beyond 2 decimals.
Example: http://jsfiddle.net/calder12/tv9HY/
Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
Edit - As mentioned by others this converts the result to a string. To avoid this:
var discount = +((price / listprice).toFixed(2));
Edit 2- As also mentioned in the comments this function fails in some precision, in the case of 1.005 for example it will return 1.00 instead of 1.01. If accuracy to this degree is important I've found this answer: https://stackoverflow.com/a/32605063/1726511 Which seems to work well with all the tests I've tried.
There is one minor modification required though, the function in the answer linked above returns whole numbers when it rounds to one, so for example 99.004 will return 99 instead of 99.00 which isn't ideal for displaying prices.
Edit 3 - Seems having the toFixed on the actual return was STILL screwing up some numbers, this final edit appears to work. Geez so many reworks!
var discount = roundTo((price / listprice), 2);
function roundTo(n, digits) {
if (digits === undefined) {
digits = 0;
}
var multiplicator = Math.pow(10, digits);
n = parseFloat((n * multiplicator).toFixed(11));
var test =(Math.round(n) / multiplicator);
return +(test.toFixed(digits));
}
See Fiddle example here: https://jsfiddle.net/calder12/3Lbhfy5s/
Edit 4 - You guys are killing me. Edit 3 fails on negative numbers, without digging into why it's just easier to deal with turning a negative number positive before doing the rounding, then turning it back before returning the result.
function roundTo(n, digits) {
var negative = false;
if (digits === undefined) {
digits = 0;
}
if (n < 0) {
negative = true;
n = n * -1;
}
var multiplicator = Math.pow(10, digits);
n = parseFloat((n * multiplicator).toFixed(11));
n = (Math.round(n) / multiplicator).toFixed(digits);
if (negative) {
n = (n * -1).toFixed(digits);
}
return n;
}
Fiddle: https://jsfiddle.net/3Lbhfy5s/79/
If you use a unary plus to convert a string to a number as documented on MDN.
For example:+discount.toFixed(2)
The functions Math.round() and .toFixed() is meant to round to the nearest integer. You'll get incorrect results when dealing with decimals and using the "multiply and divide" method for Math.round() or parameter for .toFixed(). For example, if you try to round 1.005 using Math.round(1.005 * 100) / 100 then you'll get the result of 1, and 1.00 using .toFixed(2) instead of getting the correct answer of 1.01.
You can use following to solve this issue:
Number(Math.round(100 - (price / listprice) * 100 + 'e2') + 'e-2');
Add .toFixed(2) to get the two decimal places you wanted.
Number(Math.round(100 - (price / listprice) * 100 + 'e2') + 'e-2').toFixed(2);
You could make a function that will handle the rounding for you:
function round(value, decimals) {
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
}
Example:
https://jsfiddle.net/k5tpq3pd/36/
Alternative
You can add a round function to Number using prototype. I would not suggest adding .toFixed() here as it would return a string instead of number.
Number.prototype.round = function(decimals) {
return Number((Math.round(this + "e" + decimals) + "e-" + decimals));
}
and use it like this:
var numberToRound = 100 - (price / listprice) * 100;
numberToRound.round(2);
numberToRound.round(2).toFixed(2); //Converts it to string with two decimals
Example
https://jsfiddle.net/k5tpq3pd/35/
Source: http://www.jacklmoore.com/notes/rounding-in-javascript/
To get the result with two decimals, you can do like this :
var discount = Math.round((100 - (price / listprice) * 100) * 100) / 100;
The value to be rounded is multiplied by 100 to keep the first two digits, then we divide by 100 to get the actual result.
The best and simple solution I found is
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
round(1.005, 2); // 1.01
try using discount.toFixed(2);
I think the best way I've seen it done is multiplying by 10 to the power of the number of digits, then doing a Math.round, then finally dividing by 10 to the power of digits. Here is a simple function I use in typescript:
function roundToXDigits(value: number, digits: number) {
value = value * Math.pow(10, digits);
value = Math.round(value);
value = value / Math.pow(10, digits);
return value;
}
Or plain javascript:
function roundToXDigits(value, digits) {
if(!digits){
digits = 2;
}
value = value * Math.pow(10, digits);
value = Math.round(value);
value = value / Math.pow(10, digits);
return value;
}
A small variation on the accepted answer.
toFixed(2) returns a string, and you will always get two decimal places. These might be zeros. If you would like to suppress final zero(s), simply do this:
var discount = + ((price / listprice).toFixed(2));
Edited:
I've just discovered what seems to be a bug in Firefox 35.0.1, which means that the above may give NaN with some values.
I've changed my code to
var discount = Math.round(price / listprice * 100) / 100;
This gives a number with up to two decimal places. If you wanted three, you would multiply and divide by 1000, and so on.
The OP wants two decimal places always, but if toFixed() is broken in Firefox it needs fixing first.
See https://bugzilla.mozilla.org/show_bug.cgi?id=1134388
Fastest Way - faster than toFixed():
TWO DECIMALS
x = .123456
result = Math.round(x * 100) / 100 // result .12
THREE DECIMALS
x = .123456
result = Math.round(x * 1000) / 1000 // result .123
function round(num,dec)
{
num = Math.round(num+'e'+dec)
return Number(num+'e-'+dec)
}
//Round to a decimal of your choosing:
round(1.3453,2)
Here is a working example
var value=200.2365455;
result=Math.round(value*100)/100 //result will be 200.24
To handle rounding to any number of decimal places, a function with 2 lines of code will suffice for most needs. Here's some sample code to play with.
var testNum = 134.9567654;
var decPl = 2;
var testRes = roundDec(testNum,decPl);
alert (testNum + ' rounded to ' + decPl + ' decimal places is ' + testRes);
function roundDec(nbr,dec_places){
var mult = Math.pow(10,dec_places);
return Math.round(nbr * mult) / mult;
}
I need to perform a math calculation in JavaScript that I am unsure how to do so.
The formula is as follows:
result = (scale * weighting) / 2
where the result I will need to two decimal places.
Example formula that I need to do in JavaScript might be:
result = ((3 * 2) + (2 * 1.5)) / 2
I am also unsure if I have to also convert this to int.
toFixed:
function algorithm(scale, weighting) {
var result = (scale * weighting) / 2;
return result.toFixed(2);
}
Or if you need an integer, parseInt:
parseInt(result, 10);
Not entirely sure what you want, but this might help:
result = Math.round(((scale * weighting) / 2) * 100) / 100;
http://jsfiddle.net/kQpma/
In JavaScript everything number is a floating point number. No explicit type conversion needed.
ref : http://oreilly.com/javascript/excerpts/learning-javascript/javascript-datatypes-variables.html ( search for "The Number Data Type" )