How to round down decimal number in javascript - javascript

I have this decimal number: 1.12346
I now want to keep only 4 decimals but I want to round down so it will return: 1.1234. Now it returns: 1.1235 which is wrong.
Effectively. I want the last 2 numbers: "46" do round down to "4" and not up to "5"
How is this possible to do?
var nums = 1.12346;
nums = MathRound(nums, 4);
console.log(nums);
function MathRound(num, nrdecimals) {
return num.toFixed(nrdecimals);
}

If you're doing this because you need to print/show a value, then we don't need to stay in number land: turn it into a string, and chop it up:
let nums = 1.12346;
// take advantage of the fact that
// bit operations cause 32 bit integer conversion
let intPart = (nums|0);
// then get a number that is _always_ 0.something:
let fraction = nums - intPart ;
// and just cut that off at the known distance.
let chopped = `${fraction}`.substring(2,6);
// then put the integer part back in front.
let finalString = `${intpart}.${chopped}`;
Of course, if you're not doing this for presentation, the question "why do you think you need to do this" (because it invalidates subsequent maths involving this number) should probably be answered first, because helping you do the wrong thing is not actually helping, but making things worse.

I think this will do the trick.
Essentially correcting the round up.
var nums = 1.12346;
nums = MathRound(nums, 4);
console.log(nums);
function MathRound(num, nrdecimals) {
let n = num.toFixed(nrdecimals);
return (n > num) ? n-(1/(Math.pow(10,nrdecimals))) : n;
}

This is the same question as How to round down number 2 decimal places?. You simply need to make the adjustments for additional decimal places.
Math.floor(1.12346 * 10000) / 10000
console.log(Math.floor(1.12346 * 10000) / 10000);
If you want this as a reusable function, you could do:
function MathRound (number, digits) {
var adjust = Math.pow(10, digits); // or 10 ** digits if you don't need to target IE
return Math.floor(number * adjust) / adjust;
}
console.log(MathRound(1.12346, 4));

var nums = 1.12346;
var dec = 10E3;
var intnums = Math.floor(nums * dec);
var trim = intnums / dec;
console.log(trim);

var num = 1.2323232;
converted_num = num.toFixed(2); //upto 2 precision points
o/p : "1.23"
To get the float num :
converted_num = parseFloat(num.toFixed(2));
o/p : 1.23

Related

How to get floating value of numbers in javascript?

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

Is there a simple way to split a number into an array of digits without converting it to a string and back?

I was working on a Javascript exercise that required a number to be converted into an array of single digits and the only way I can seem to achieve this is by converting the number into a string and then converting it back to a number.
let numbers = 12345;
Array.from(numbers.toString(10), Number) // [1, 2, 3, 4, 5]
Basically, I'm wondering if this is the best way to achieve a split like this on a number or if there is a more efficient method that doesn't require such a conversion.
You can always get the smallest digit with n % 10. You can remove this digit with subtraction and division by 10 (or divide and floor). This makes for a pretty simple loop:
function digits(numbers){
if (numbers == 0) return [numbers]
let res = []
while (numbers){
let n = numbers % 10
res.push(n)
numbers = (numbers - n) / 10
}
return res.reverse()
}
console.log(digits(1279020))
This takes the numbers in reverse order so you either have to unshift the results on to the array or push and reverse at the end.
One of the nice things about this, is that you can find the digits of different bases by swapping out 10 for a the base of your choice:
function digits(numbers, base){
if (numbers == 0) return [numbers]
let res = []
while (numbers){
let n = numbers % base
res.push(n)
numbers = (numbers - n) / base
}
return res.reverse()
}
// binary
console.log(digits(20509, 2).join(''))
console.log((20509).toString(2))
// octal
console.log(digits(20509, 8).join(''))
console.log((20509).toString(8))
Although once your base is larger than 10 you will have to map those digits to the appropriate letters.
One approach would be to iterate through the number of digits and calculate the difference of each modulo by base, and then populate the output list from the result of each iteration.
A quick way to identify the number of digits in your base 10 input would be the following:
Math.floor(Math.log(input) / Math.LN10 + 1) // 5 for input of 12349
Next, iterate through this range and for each iteration, calculate the base of the current and previous iterations, and perform module of the input against these. The digit for the current iteration is then derived from the difference of the modulo calculations like this:
function arrayFromInput(input) {
const output = [];
for (let i = 0; i < Math.floor(Math.log(input) / Math.LN10 + 1); i++) {
const lastBase = Math.pow(10, i);
const nextBase = Math.pow(10, i + 1);
const lastMod = input % lastBase;
const nextMod = input % nextBase;
const digit = (nextMod - lastMod) / lastBase;
output.unshift(digit);
}
return output;
}
console.log(arrayFromInput(12345), '= [1,2,3,4,5]');
console.log(arrayFromInput(12), '= [1,2]');
console.log(arrayFromInput(120), '= [1,2 0]');
console.log(arrayFromInput(9), '= [9]');
console.log(arrayFromInput(100), '= [1,0,0]');

How do i return a number excluding the last digit?

So I have a number like 5467. I want my code to return 546.
I tried taking the last number and subtracting it from the original number but I get 5460 instead of 546.
Combine / with %:
(5467 - (5467 % 10)) / 10
564
Sounds like you also need to divide my 10. You could do something like this:
var number = 5467;
number = number - (number % 10); // This will subtract off the last digit.
number = number / 10;
console.log(number); // 546
We first use the modulo operator % to get the last digit, and we subtract it from number. That reduces the number from 5467 to 5460. Now to chop off the last digit (which is guaranteed to be a 0) we divide by 10 and get 546.
Written more concisely you could do:
number = (number - ( number % 10)) / 10;
There's a few things you can do the most concise being:
Math.floor(num / 10);
Or, convert to a string, remove the last character and convert back to number.
parseInt(num.toString().slice(0, -1));
If string representation would be fine for you then one other way is
var num = 5467,
cut = (num/10).toFixed(); // <-'547'
Well... warning..! i have to say toFixed() method rounds if necessary. So in this particular example it doesn't work.
I dont mind some of the other answers, but i feel that this maybe too fixed on it being a number.
Which it is, but you want to remove the last digit/char, regardless of the number, so why not substr?
http://www.w3schools.com/jsref/jsref_substr.asp
var s = 5467;
s = s.toString().substr(0, s.toString().length - 1);
console.log(s)
or even easier:
var s = (5467).toString();
s = s.substr(0, s.length - 1);
console.log(s)
These dont take into account single digit numbers, so passing in 1 would return blank. To answer that you could simply do a check like:
var s = (1).toString();
if(s.length > 1)
s = s.substr(0, s.length - 1);
console.log(s)
Also, similar question to:
Remove last digits from an int
Remove the last digits of a number (not string)
Removing the last digits in string
To truncate digits from the right hand side until the number is less than 30, keep dividing by 10 and rounding down until a suitable value is reached:
var n = 12341235;
while (n > 30) n = n/10|0;
document.write(n);
The greater than and division operations will coerce n to a number, so it can be a number or string. If ToNumber(n) results in NaN (e.g. n = 'foo'), then the value of n is not modified.
You can simply divide the number by 10 and parseInt()
var num = 5467;
num = parseInt(num/10);
Update :
To repeat the process until the answer is less than 30, use while loop as
var num = 5467;
while(num >= 30) {
num = parseInt(num/10);
}
document.write(num);

Math.round Rounding Error

I Want to round 1.006 to two decimals expecting 1.01 as output
When i did
var num = 1.006;
alert(Math.round(num,2)); //Outputs 1
alert(num.toFixed(2)); //Output 1.01
Similarly,
var num =1.106;
alert(Math.round(num,2)); //Outputs 1
alert(num.toFixed(2));; //Outputs 1.11
So
Is it safe to use toFixed() every time ?
Is toFixed() cross browser complaint?
Please suggest me.
P.S: I tried searching stack overflow for similar answers, but could not get proper answer.
EDIT:
Why does 1.015 return 1.01 where as 1.045 returns 1.05
var num =1.015;
alert(num.toFixed(2)); //Outputs 1.01
alert(Math.round(num*100)/100); //Outputs 1.01
Where as
var num = 1.045;
alert(num.toFixed(2)); //Outputs 1.04
alert(Math.round(num*100)/100); //Outputs 1.05
Try something like...
Math.round(num*100)/100
1) Multiple the original number by 10^x (10 to the power of x)
2) Apply Math.round() to the result
3) Divide result by 10^x
from: http://www.javascriptkit.com/javatutors/round.shtml
(to round any number to x decimal points)
This formula Math.round(num*100)/100 is not always good. Example
Math.round(0.145*100)/100 = 0.14
this is wrong, we want it to be 0.15
Explanation
The problem is that we have floats like that
0.145 * 100 = 14.499999999999998
step one
so If we round, we need to add a little bit to our product.
0.145 * 100 + 1e-14 = 14.500000000000009
I assume that sometimes the product might be something like 1.000000000000001, but it would not be a problem if we add to it, right?
step two
Calculate how much should we add?
We know float in java script is 17 digits.
let num = 0.145
let a = Math.round(num*100)/100
let b = a.toString().length
let c = 17-b-2
let result = Math.round(num*100 + 0.1**c)/100
console.log(result)
console.log('not - ' + a )
(-2) - is just to be sure we are not falling into the same trap of rounding.
One-liner:
let num = 0.145
let result = Math.round(num*100 + 0.1**(17-2-(Math.round(num*100)/100).toString().length))/100
Extras
Remember, that everything above is true for positive numbers. If you rounding negative number you would need to subtract a little bit. So the very final One-liner would be:
let num = -0.145
let result = Math.round(num*100 + Math.sign(num)*0.1**(17-2-(Math.round(num*100)/100).toString().length))/100
I realize this problem is rather old, but I keep running into it even 5 years after the question has been asked.
A working solution to this rounding problem I know of is to convert the number to a string, get the required precision number and round up or down using math rules.
An example where Math.round provides unexpected rounding and an example of string rounding can be found in the following fiddle:
http://jsfiddle.net/Shinigami84/vwx1yjnr/
function round(number, decimals = 0) {
let strNum = '' + number;
let negCoef = number < 0 ? -1 : 1;
let dotIndex = strNum.indexOf('.');
let start = dotIndex + decimals + 1;
let dec = Number.parseInt(strNum.substring(start, start + 1));
let remainder = dec >= 5 ? 1 / Math.pow(10, decimals) : 0;
let result = Number.parseFloat(strNum.substring(0, start)) + remainder * negCoef;
return result.toFixed(decimals);
}
let num = 0.145;
let precision = 2;
console.log('math round', Math.round(num*Math.pow(10, precision))/Math.pow(10, precision));
// 0.145 rounded down to 0.14 - unexpected result
console.log('string round', round(num, precision));
// 0.145 rounded up to 0.15 - expected result
Math.round doesn't work properly here because 0.145 multiplied by 100 is 14.499999999999998, not 14.5. Thus, Math.round will round it down as if it was 14.4. If you convert it to a string and subtract required digit (5), then round it using standard math rules, you will get an expected result of 0.15 (actually, 0.14 + 0.01 = 0.15000000000000002, use "toFixed" to get a nice, round result).

Convert Fraction String to Decimal?

I'm trying to create a javascript function that can take a fraction input string such as '3/2' and convert it to decimal—either as a string '1.5' or number 1.5
function ratio(fraction) {
var fraction = (fraction !== undefined) ? fraction : '1/1',
decimal = ??????????;
return decimal;
});
Is there a way to do this?
Since no one has mentioned it yet there is a quick and dirty solution:
var decimal = eval(fraction);
Which has the perks of correctly evaluating all sorts of mathematical strings.
eval("3/2") // 1.5
eval("6") // 6
eval("6.5/.5") // 13, works with decimals (floats)
eval("12 + 3") // 15, you can add subtract and multiply too
People here will be quick to mention the dangers of using a raw eval but I submit this as the lazy mans answer.
Here is the bare bones minimal code needed to do this:
var a = "3/2";
var split = a.split('/');
var result = parseInt(split[0], 10) / parseInt(split[1], 10);
alert(result); // alerts 1.5
JsFiddle: http://jsfiddle.net/XS4VE/
Things to consider:
division by zero
if the user gives you an integer instead of a fraction, or any other invalid input
rounding issues (like 1/3 for example)
Something like this:
bits = fraction.split("/");
return parseInt(bits[0],10)/parseInt(bits[1],10);
I have a function I use to handle integers, mixed fractions (including unicode vulgar fraction characters), and decimals. Probably needs some polishing but it works for my purpose (recipe ingredient list parsing).
NPM
GitHub
Inputs "2 1/2", "2½", "2 ½", and "2.5" will all return 2.5. Examples:
var numQty = require("numeric-quantity");
numQty("1 1/4") === 1.25; // true
numQty("3 / 4") === 0.75; // true
numQty("¼" ) === 0.25; // true
numQty("2½") === 2.5; // true
numQty("¾") === 0.75; // true
numQty("⅓") === 0.333; // true
numQty("⅔") === 0.667; // true
One thing it doesn't handle is decimals within the fraction, e.g. "2.5 / 5".
I created a nice function to do just that, everything was based off of this question and answers but it will take the string and output the decimal value but will also output whole numbers as well with out errors
https://gist.github.com/drifterz28/6971440
function toDeci(fraction) {
fraction = fraction.toString();
var result,wholeNum=0, frac, deci=0;
if(fraction.search('/') >=0){
if(fraction.search('-') >=0){
wholeNum = fraction.split('-');
frac = wholeNum[1];
wholeNum = parseInt(wholeNum,10);
}else{
frac = fraction;
}
if(fraction.search('/') >=0){
frac = frac.split('/');
deci = parseInt(frac[0], 10) / parseInt(frac[1], 10);
}
result = wholeNum+deci;
}else{
result = fraction
}
return result;
}
/* Testing values / examples */
console.log('1 ',toDeci("1-7/16"));
console.log('2 ',toDeci("5/8"));
console.log('3 ',toDeci("3-3/16"));
console.log('4 ',toDeci("12"));
console.log('5 ',toDeci("12.2"));
Too late, but can be helpful:
You can use Array.prototype.reduce instead of eval
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
ES6
const fractionStrToDecimal = str => str.split('/').reduce((p, c) => p / c);
console.log(fractionStrToDecimal('1/4/2')); // Logs 0.125
console.log(fractionStrToDecimal('3/2')); // Logs 1.5
CJS
function fractionStrToDecimal(str) {
return str.split('/').reduce((p, c) => p / c);
}
console.log(fractionStrToDecimal('1/4')); // Logs 0.25
[EDIT] Removed reducer initial value and now the function works for numerators greater than 1. Thanks, James Furey.
Function (ES6):
function fractionToDecimal(fraction) {
return fraction
.split('/')
.reduce((numerator, denominator, i) =>
numerator / (i ? denominator : 1)
);
}
Function (ES6, condensed):
function fractionToDecimal(f) {
return f.split('/').reduce((n, d, i) => n / (i ? d : 1));
}
Examples:
fractionToDecimal('1/2'); // 0.5
fractionToDecimal('5/2'); // 2.5
fractionToDecimal('1/2/2'); // 0.25
fractionToDecimal('10/5/10'); // 0.2
fractionToDecimal('0/1'); // 0
fractionToDecimal('1/0'); // Infinity
fractionToDecimal('cat/dog'); // NaN
With modern destructuring syntax, the best/safest answer can be simplified to:
const parseFraction = fraction => {
const [numerator, denominator] = fraction.split('/').map(Number);
return numerator / denominator;
}
// example
parseFraction('3/2'); // 1.5
In other words, split the faction by its / symbol, turn both resulting strings into numbers, then return the first number divided by the second ...
... all with only two (very readable) lines of code.
If you don't mind using an external library, math.js offers some useful functions to convert fractions to decimals as well as perform fractional number arithmetic.
console.log(math.number(math.fraction("1/3"))); //returns 0.3333333333333333
console.log(math.fraction("1/3") * 9) //returns 3
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.20.1/math.js"></script>
const fractionStringToNumber = s => s.split("/").map(s => Number(s)).reduce((a, b) => a / b);
console.log(fractionStringToNumber("1/2"));
console.log(fractionStringToNumber("1/3"));
console.log(fractionStringToNumber("3/2"));
console.log(fractionStringToNumber("3/1"));
console.log(fractionStringToNumber("22/7"));
console.log(fractionStringToNumber("355 / 113"));
console.log(fractionStringToNumber("8/4/2"));
console.log(fractionStringToNumber("3")); // => 3, not "3"
From a readability, step through debugging perspective, this may be easier to follow:
// i.e. '1/2' -> .5
// Invalid input returns 0 so impact on upstream callers are less likely to be impacted
function fractionToNumber(fraction = '') {
const fractionParts = fraction.split('/');
const numerator = fractionParts[0] || '0';
const denominator = fractionParts[1] || '1';
const radix = 10;
const number = parseInt(numerator, radix) / parseInt(denominator, radix);
const result = number || 0;
return result;
}
To convert a fraction to a decimal, just divide the top number by the bottom number. 5 divided by 3 would be 5/3 or 1.67. Much like:
function decimal(top,bottom) {
return (top/bottom)
}
Hope this helps, haha
It works with eval() method but you can use parseFloat method. I think it is better!
Unfortunately it will work only with that kind of values - "12.2" not with "5/8", but since you can handle with calculation I think this is good approach!
If you want to use the result as a fraction and not just get the answer from the string, a library like https://github.com/infusion/Fraction.js would do the job quite well.
var f = new Fraction("3/2");
console.log(f.toString()); // Returns string "1.5"
console.log(f.valueOf()); // Returns number 1.5
var g = new Fraction(6.5).div(.5);
console.log(f.toString()); // Returns string "13"
Also a bit late to the party, but an alternative to eval() with less security issues (according to MDN at least) is the Function() factory.
var fraction = "3/2";
console.log( Function("return (" + fraction + ");")() );
This would output the result "1.5" in the console.
Also as a side note: Mixed fractions like 1 1/2 will not work with neither eval() nor the solution with Function() as written as they both stumble on the space.
safer eval() according to MDN
const safeEval = (str) => {
return Function('"use strict";return (' + str + ")")();
}
safeEval("1 1/2") // 1.5
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Do_not_ever_use_eval!
This too will work:
let y = "2.9/59"
let a = y.split('')
let b = a.splice(a.indexOf("/"))
console.log(parseFloat(a.join('')))
a = parseFloat(a.join(''))
console.log(b)
let c = parseFloat(b.slice(1).join(''))
let d = a/c
console.log(d) // Answer for y fraction
I developed a function to convert a value using a factor that may be passed as a fraction of integers or decimals. The user input and conversion factor might not be in the correct format, so it checks for the original value to be a number, as well as that the conversion can be converted to a fraction assuming that /number means 1/number, or there are a numerator and a denominator in the format number/number.
/**
* Convert value using conversion factor
* #param {float} value - number to convert
* #param {string} conversion - factor
* #return {float} converted value
*/
function convertNumber(value, conversion) {
try {
let numberValue = eval(value);
if (isNaN(numberValue)) {
throw value + " is not a number.";
}
let fraction = conversion.toString();
let divider = fraction.indexOf("/");
let upper = 1;
let denominator = 1;
if (divider == -1) {
upper = eval(fraction);
} else {
let split = fraction.split("/");
if (split.length > 2) {
throw fraction + " cannot be evaluated to a fraction.";
} else {
denominator = eval(split[1]);
if (divider > 0) {
upper = eval(split[0]);
}
}
}
let factor = upper/denominator;
if (isNaN(factor)) {
throw fraction + " cannot be converted to a factor.";
}
let result = numberValue * factor;
if (isNaN(result)) {
throw numberValue + " * " + factor + " is not a number.";
}
return result
} catch (err) {
let message = "Unable to convert '" + value + "' using '" + conversion + "'. " + err;
throw message;
}
}
You can use eval() with regex to implement a secure method to calculate fraction
var input = "1/2";
return input.match(/^[0-9\/\.]+$/) != null ? eval(input) : "invalid number";

Categories