Round Down Decimal Places Javascript - javascript

I need a rounding down many many decimal places down, basically roundTo but supposedly rounding down at the spot. Example,
take the number, 1.087179939485353505
but to the fifth place, with roundTo of 6
roundTo(1.087179939485353505, 6) is 1.08718
I need 1.08717 not 1.08718 in javascript.
var Variable = roundTo(Variable / 1000000000000000000, 6);
Resolved
There seems to be no native javascript decimal rounding function that rounds down. One of two options are available.
Convert to string and manipulate the data that way (makes the most sense).
Utilize a number and multiply, floor then re-divide again for your number

How about convert to string, slice and convert back to number.
const roundTo = num => Number(String(num).slice(0, 7));
console.log(roundTo(1.087179939485353505));

You could use regex to get 5 decimals
function roundTo(number) {
var result= number.toString().match(/^\d+(\.\d{0,5})/)[0];
console.log(result);
}
roundTo(1.087179939485353505);

var Variable = Variable / 1000000000000000000;
Variable *= 1000000;
Variable = Math.floor(Variable);
Variable /= 1000000;
var Variable = roundTo(Variable, 6);
I took my decimal, multiplied it by how many places I wanted to roundTo, math floor for absolute low rounding and divided it once again before lastly using roundTo for the precise decimal place. Seems the only way.

Related

Math.sin() Different Precision between Node.js and C#

I have a problem in precision in the last digit after the comma.The javascript code generates one less Digit in compare with the C# code.
Here is the simple Node.js code
var seed = 45;
var x = Math.sin(seed) * 0.5;
console.log(x);//0.4254517622670592
Here is the simple C# code
public String pseudorandom()
{
int seed = 45;
double num = Math.Sin(seed) * (0.5);
return num.ToString("G15");//0.42545176226705922
}
How to achieve the same precision?
The JavaScript Number type is quite complex. It looks like floating point number will probably be like IEEE 754-2008 but some aspects are left to the implementation. See http://www.ecma-international.org/ecma-262/6.0/#sec-number-objects sec 12.7.
There is a note
The output of toFixed may be more precise than toString for some
values because toString only prints enough significant digits to
distinguish the number from adjacent number values. For example,
(1000000000000000128).toString() returns "1000000000000000100", while
(1000000000000000128).toFixed(0) returns "1000000000000000128".
Hence to get full digit accuracy you need something like
seed = 45;
x = Math.sin(seed) * 0.5;
x.toFixed(17);
// on my platform its "0.42545176226705922"
Also, note the specification for how the implementation of sin and cos allow for some variety in the actual algorithm. It's only guaranteed to within +/- 1 ULP.
Using java the printing algorithm is different. Even forcing 17 digits gives the result as 0.42545176226705920.
You can check you are getting the same bit patterns using x.toString(2) and Double.doubleToLongBits(x) in Java.
return num.ToString("G15");//0.42545176226705922
actually returns "0.425451762267059" (no significant digit + 15 decimal places in this example), and not the precision shown in the comment after.
So you would use:
return num.ToString("G16");
to get "0.4254517622670592"
(for your example - where the significant digit is always 0) G16 will be 16 decimal places.

Obtain two decimal places in JavaScript without rounding to the next bigger number

I have this JS code:
var propertyYield = annualRent / propertyValue * 100.0;
In specific scenario the result is 4.999.
So when I do this
propertyYield.toFixed(2)
I'm getting propertyYield of 5.00.
What I want to achieve is to actually get 4.99 instead of 5.00 as rounding to two decimals.
How can I achieve that?
You could use Math.floor and some additional arithmetics:
Math.floor(15.7784514000 * 100) / 100
Or convert the number into a string, match the number up to the second decimal place and turn it back into a number:
Number(15.7784514000.toString().match(/^\d+(?:\.\d{0,2})?/))
Then you can still call toFixed to get a string with a fixed number of decimal places.
var num1 = Math.floor(15.7784514000 * 100) / 100;
console.log(num1);
var num2 = Number(15.7784514000.toString().match(/^\d+(?:\.\d{0,2})?/));
console.log(num2)
console.log(num2.toFixed(2))
Update: As noted by #kuka this does not work for certain decimal numbers due to floating point error math. Don't use this solution - However I'm leaving it here for documentation sake.
Not sure if I know a library method off hand to do that but quick simple old school solution would be this:
Math.floor(4.999 * 100) / 100.0

Converting floating point numbers to integers, rounding to 2 decimals in JavaScript

What's the best way to perform the following conversions in JavaScript? I have currencies stored as floats that I want rounded and converted to integers.
1501.0099999999999909 -> 150101
12.00000000000001 -> 1200
One way to do this is to use the toFixed method off a Number combined with parseFloat.
Eg,
var number = 1501.0099999999999909;
var truncated = parseFloat(number.toFixed(5));
console.log(truncated);
toFixed takes in the number of decimal points it should be truncated to.
To get the output you need, you would only need `toFixed(2)' and multiple the result by 100.
Eg,
var number = 1501.0099999999999909;
var truncated = parseFloat(number.toFixed(2)) * 100;
console.log(truncated);

I need to round the number of a field

I have tried many things about the rounding of a filed but I get NaN, here is what I am trying to round up to 999999,9
document.forms[0].NB_CONCN_MOY_DCO_MS
I have tried Math.round(document.forms[0].NB_CONCN_MOY_DCO_MS.value)
and
document.forms[0].Math.round(NB_CONCN_MOY_DCO_MS.value)
what can I do now.
If you want to round a number to one decimal place in JavaScript, use someNumber.toFixed(1). Note that the value of form fields is a string (not a number) so you'll want to convert it to a number first.
var n = document.forms[0].NB_CONCN_MOY_DCO_MS.value * 1;
var rounded = n.toFixed(1);
If your value has commas in it to represent decimal values, you will need to fix the string to use periods instead first:
var n = document.forms[0].NB_CONCN_MOY_DCO_MS.value.replace(/,/,',') * 1;
var rounded = n.toFixed(1);

Rounding the result of division in Javascript

I'm performing the following operation in Javascript:
0.0030 / 0.031
How can I round the result to an arbitrary number of places? What's the maximum number that a var will hold?
Modern browsers should support a method called toFixed(). Here's an example taken from the web:
// Example: toFixed(2) when the number has no decimal places
// It will add trailing zeros
var num = 10;
var result = num.toFixed(2); // result will equal 10.00
// Example: toFixed(3) when the number has decimal places
// It will round to the thousandths place
num = 930.9805;
result = num.toFixed(3); // result will equal 930.981
toPrecision() might also be useful for you, there is another excellent example on that page.
For older browsers, you can achieve it manually using Math.round. Math.round() will round to the nearest integer. In order to achieve decimal precision, you need to manipulate your numbers a bit:
Multiply the original number by 10^x
(10 to the power of x), where x is
the number of decimal places you
want.
Apply Math.round()
Divide by 10^x
So to round 5.11111111 to three decimal places, you would do this:
var result=Math.round(5.111111*1000)/1000 //returns 5.111
The largest positive finite value of the number type is approximately 1.7976931348623157 * 10308. ECMAScript-262 3rd ed. also defines Number.MAX_VALUE which holds that value.
To answer Jag's questions:
Use the toFixed() method. Beware; it returns a string, not a number.
Fifteen, maybe sixteen. If you try to get more, the extra digits will be either zeros or garbage. Try formatting something like 1/3 to see what I mean.

Categories