Split amount with defined precision and no rounding errors - javascript

I needed a way to split an amount (in dollars) between one or more "buckets", for lack of a better word. It needed to have the standard two decimal precision a dollar amount should, but every penny needed to be accounted for.
To illustrate the problem, consider this example:
$200 split 3 ways. If you round down, each amount is $66.66 and you end up 2 cents short, but if you round up, each amount is $66.67 and you're 2 cents over. The answer, of course, is to have one $66.66 and the other two $66.67. That's easy enough for a human to reason, but trying to make bit of JavaScript understand that is kind of a challenge.

Here's the function I came up with:
function SplitTotal( totalAmount, numberToSplit, precision ) {
var precisionFactor = Math.pow(10, precision);
var remainderFactor = 1 / precisionFactor;
var splitAmount = Math.ceil( totalAmount / numberToSplit * precisionFactor ) / precisionFactor;
var remainder = Math.round( (splitAmount * numberToSplit - totalAmount) * precisionFactor ) / precisionFactor;
var result = [];
for (var i = 0; i < numberToSplit; i++) {
result.push( (remainder >= remainderFactor) ?
Math.round( (splitAmount - remainderFactor) * precisionFactor ) / precisionFactor :
splitAmount );
remainder = Math.round( (remainder - remainderFactor) * precisionFactor ) / precisionFactor;
}
return result;
}
The code is a little convoluted, because JavaScript handles floating point numbers atrociously. In order to get a nice, precise decimal amount, you need to multiply the amount by what I call here a "precision factor", which is just some exponent of 10, round the resulting number, and then divide by that precision factor to get back to the original decimal precision.
Even though I only needed 2 decimal spaces of precision, I made the precision easily definable, so this can work for more applications that just dollar amounts. If you want just integers, you can specify a precision of 0.
Ex: $235.38 divided 7 ways:
> SplitTotal(235.38, 7, 2)
[33.62, 33.62, 33.62, 33.63, 33.63, 33.63, 33.63]

Related

Javascript round / floor / toFixed on decimals

I am having an issue with how javascript is dividing and rounding the number.
I have two float , 0.11 and 0.12
I want to calculate the mid of these two numbers and round it to the nearest highest value with 2 decimal price.
For example, if I do this on Calculator
0.11+0.12 / 2 = 0.115, and I need to round it to 0.12 as it is mid or above mid.
If I do this with Javascript, I am not getting an accurate number
Example,
var high = parseFloat(0.12);
var low = parseFloat(0.11);
var mid = (high + low) / 2;
document.getElementById("demo1").innerHTML = mid;
document.getElementById("demo2").innerHTML = mid.toFixed(2);
var another = mid.toFixed(3);
document.getElementById("demo3").innerHTML =another;
var last = Math.floor(another)
document.getElementById("demo4").innerHTML =last;
http://jsfiddle.net/gzqwbp6c/9/
Any input would be appreciated.
As the 0.11499999999999999 shows, the result is very slightly less than 0.115. This is because 0.11 and 0.12 cannot be represented with perfect accuracy using floating-point-numbers.
When you don't want to deal with floating-point-error, it's often easier to work with integers directly. Small integers are represented exactly by floating point numbers.
You can multiply by 100 before, and round, to ensure your numbers are integers, and only divide after you get your final result:
var a = Math.round(100 * parseFloat("0.12")) // 12
var b = Math.round(100 * parseFloat("0.11")) // 11
var mid = (a + b) / 2 // 11.5.
// 0.5 can be represented exactly in floating point for small numbers.
var midRound = (Math.round(mid) / 100).toFixed(2) // "0.12"
Need to multiply (workout on int part, i.e. find mid, and divide to reconvert to origin):
function myMid(high,low, precision){
var precision=2
var convFactor = Math.pow(10,precision);
return
(Math.round((low*convFactor+high*convFactor)/2)/convFactor).toFixed(precision);
}
Float is not precise, you cant rely on that, you'll have unexpected results.
everything *100 to prevent inaccuracies
.toFixed() does the rounding
var a = 0.11;
var b = 0.12;
c = parseFloat((((a*100) + (b*100))/200).toFixed(2));
console.log(c);

Precision issue with Decimal.js tan

I have been using Decimal.js to increase the precision of my function that calculates the mth positive root of a = tan(a) through trial and error. It works, however it returns a "Precision limit exceeded" error for nTan(504) (would return 4.4934... to 505 digits) and greater.
var Decimal = require("decimal.js");
var fs = require("fs");
function nTan (acc, m) {
var test = [1], acc = (parseInt(acc) || 15) + 1;
Decimal.set({precision: acc});
var n = new Decimal(fs.readFileSync("result.txt", "utf-8") || 4.4).toString();
while (n.length + test.length - 2 < acc) {
var dec = (new Decimal(n + test.join("")));
if (dec.tan().cmp(n + test.join("")) >= 0) {
test[test.length - 1]--;
test.push(1);
} else test[test.length - 1]++;
if (test[test.length - 1] == 10) { test[test.length - 1] = 9; test.push(1); }
}
return (new Decimal(n + test.slice(0, -1).join(""))).plus(Math.PI * (parseInt(m) || 0)).toString();
}
My question(s) are:
Why won't Decimal.js calculate past 504 digits when it advertises the capacity for up to and including 1e+9 digits?
Is there an alternative node or JS API that would support this program to a greater precision?
1000000000 is the maximum permitted value for the decimal.js precision setting, but that does not mean that the trigonometric methods can return a result to that number of significant digits.
The limit to the precision of the trigonometric methods is determined by the precision of the value of Pi in the source code. It is hard-coded in the decimal.js file as the string variable PI, and has a precision of 1025 digits.
This means that the precision limit for the cos, sin and tan methods is up to about 1000 digits, but the actual figure depends on the precision of the argument passed to them. To calculate the actual figure use
maximum_result_precision = 1000 - argument_precision
For example, the following both work fine
Decimal.set({precision: 991}).tan(123456789);
Decimal.set({precision: 9}).tan(991_digit_number);
as, for each, the result precision plus the argument precision, i.e. 991 + 9 and 9 + 991, is less than or equal to 1000.
This is why your program fails when you try and calculate the tan of an argument with more than 500 digits to a precision of more than 500 digits.
To do it would require Pi to a higher precision - and that can only be done, and can be done simply, by editing the value of PI in the source code, i.e. add more digits to it. The time taken by the methods will then be the limiting factor.
I am the library's author and I need to add this to its documentation.

toFixed method without rounding to five digit

I have a number var x = 2.305185185185195;
x = x.toFixed(5);
x = 2.30519 but I require this without rounding i.e. 2.30518
I read some thread with two decimal places but could not find for five decimal places.
Any help would be appreciated.
You can use an apropriate factor and floor it and return the result of the division.
Basically this solution moves the point to the left with a factor of 10^d and gets an integer of that and divided the value with the former factor to get the right digits.
function getFlooredFixed(v, d) {
return (Math.floor(v * Math.pow(10, d)) / Math.pow(10, d)).toFixed(d);
}
var x = 2.305185185185195;
document.write(getFlooredFixed(x, 5));
If you need only a "part" of a number with a floating point without rounding, you can just "cut" it:
function cutNumber(number, digitsAfterDot) {
const str = `${number}`;
return str.slice(0, str.indexOf('.') + digitsAfterDot + 1);
}
const x = 2.305185185185195;
console.log(cutNumber(x, 5)); // 2.30518
This method is fast (https://jsfiddle.net/93m8akzo/1/) and its execution time doesn't depend on number or digitsAfterDot values.
You can also "play around" with both functions in a given fiddle for a better understanding of what they do.
You can read more about slice() method here - MDN documentation
NOTE This function is only an example, don't use it in production applications.
You should definitely add input values validation and errors handling!
The Math.trunc() function returns the integer part of a number by
removing any fractional digits
So you can multiply the number by 10^n where n is the desired number of precision, truncate the decimal part using Math.trunc(), divide by the same number (10^n) and apply toFixed() to format it (in order to get the form of 2.30 instead of 2.3 for example)
var x = 2.305185185185195;
console.log((Math.trunc(x*100000)/100000).toFixed(5));
I have sorted it out by adding a small amount if the decimal is 5, then rounding as usual:
function(value, decimals) {
var decimals = decimals || 2;
if( isNaN(value) ){ return 0; }
var decimalPart = value.toString().trim().split('.').pop(),
extra = decimalPart.substr(decimals, decimalPart.length - decimals);
if( extra == '5' &&
decimalPart.length > decimals
){
value = parseFloat(value) + (1 / ( Math.pow(10, decimals + 5) ) );
}
return Number( parseFloat( value ).toFixed( decimals ) );
}

Convert repeating decimal to fraction

I am using a button on a JavaScript scientific calculator to convert decimal to fractions via this code:
$('#button-frac').click(function(){
var factor;
// Finds the highest common factor of 2 numbers
function highestCommonFactor() {
for (factor = numerator; factor > 0; factor--) {
if ((numerator % factor == 0) && (denominator % factor == 0)) {
return factor;
}
}
}
// Enter a decimal to convert to a fraction
var decimal = this.form.display.value;
// Split the decimal
var decimalArray = decimal.split(".");
var leftDecimalPart = decimalArray[0];
var rightDecimalPart = decimalArray[1];
// Save decimal part only for later use
var decimalOnly = "0." + rightDecimalPart;
// Find the decimal multiplier
var multiplier = "1";
for (var i = 0; i < rightDecimalPart.length; i++) {
multiplier += "0";
}
// Create numerator by multiplying the multiplier and decimal part together
var numerator = Number(multiplier) * Number(decimalOnly);
var denominator = multiplier;
// Find the highest common factor for the numerator and denominator
highestCommonFactor();
// Simplify the fraction by dividing the numerator and denominator by the factor
var numerator = Number(numerator) / Number(factor);
var denominator = Number(denominator) / Number(factor);
// Output as a mixed number fraction (depending on input)
var mixedNumber = leftDecimalPart + " " + numerator + "/" + denominator;
// Output as a proper fraction or improper fraction (depending on input)
var numerator = numerator + (leftDecimalPart * denominator);
var fraction = numerator + "/" + denominator;
// Display solution in input #disp
$('#disp').val(fraction);
});
This works well, but if the decimal is non-terminating and repeating the script crashes. Any idea how I might remedy this problem? Perhaps there's a way to check if a decimal repeats and to determine the length of the string that repeats, then take that string and express it over a number with equal number of digits, all 9s? Being relatively new to JavaScript, I am at a loss.
Use this and round the decimal after at most 20 repeated chars.
Math.fraction=function(x){
return x?+x?x.toString().includes(".")?x.toString().replace(".","")/(function(a,b){return b?arguments.callee(b,a%b):a;})(x.toString().replace(".",""),"1"+"0".repeat(x.toString().split(".")[1].length))+"/"+("1"+"0".repeat(x.toString().split(".")[1].length))/(function(a,b){return b?arguments.callee(b,a%b):a;})(x.toString().replace(".",""),"1"+"0".repeat(x.toString().split(".")[1].length)):x+"/1":NaN:void 0;
}
Call it with Math.fraction(2.56)
It will:
return NaN if the input is not a number
return undefined if the input is undefined
reduce the fraction
return a string (use Math.fraction(2.56).split("/") for an array containing the numerator and denominator)
Please note that this uses the deprecated arguments.callee, and thus may be incompatible in some browsers.
Test it here

How to make javascript round sensitive to number of digits in value?

Say we had an array [0.09, 870, 499] and we want to get array values round so: [0.1, 1000, 100]?
What have I tried:
var logarithmicRound = function(val) {
var degree = Math.round(Math.log(val) / Math.LN10);
if(Math.pow(10, degree) - val > val) {
--degree;
}
return Math.pow(10, degree);
};
console.log(logarithmicRound(0.05));
console.log(logarithmicRound(0.7));
console.log(logarithmicRound(49));
console.log(logarithmicRound(50));
console.log(logarithmicRound(400));
console.log(logarithmicRound(800));
// prints
//0.1
//1
//10
//100
//100
//1000
Yet it seems quite ugly... yet it does exactly what I need.
I use a couple of functions for rounding numbers, they might be useful.
function roundTo2(value){
return (Math.round(value * 100) / 100);
}
function roundResult(value, places){
var multiplier = Math.pow(10, places);
return (Math.round(value * multiplier) / multiplier);
}
You'll obviously need to round numbers and put into the array / extract, round, put back - not as efficient as someone elses answer may be
Assuming that you wish to round up to the nearest power of 10 (and that your example of 499 rounding to 100 is incorrect):
var rounded = myArray.map(function(n) {
return Math.pow(10, Math.ceil(Math.log(n) / Math.LN10));
});
From the given example it looks like #DuckQueen wants to round off to nearest power of 10..
Here is the algo -
1. Represent each number N in scientific notation S. Lets say S is n*10^x
2. Let A =(N - (10 power x)) and B=((10 pow x+1) - N)
3. if A<B N = 10^x otherwise N=10^(x+1)
You may assume one way or the other for the case A==B
Use this for Step 1:
How can I convert numbers into scientific notation?

Categories

Resources