How to round numbers with N digits after the zero? [duplicate] - javascript

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 6 years ago.
lets say I have a number in the following form: 0.00N1N2N3...(for example 0.007).
I want to round the number 0.00N1N2N3...Nn, into the follwoing number:
0.0M1M2M3..Mn.
For example:0.007 need to be round to 0.01.
Now the number can be also in the following form 0.N1...Nn or N1.N2...Nn so the solution need to be generic for all cases.
I have write the following function(Not sure if this is the right answer):
function roundup(number, precision) {
return Math.ceil(number * precision)
}

If the variable is float you can use toFixed() like
var formatted = parseFloat("345.65894").toFixed(2);

On most browsers you can use the toFixed() function.
number.toFixed(precision)
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Related

Why does subtracting decimals not work in javascript [duplicate]

This question already has answers here:
difference between parseInt() and parseFloat() [duplicate]
(2 answers)
Closed 10 months ago.
If I do the code below and enter a decimal for one or both of the numbers, lets says I use 0.5 and 0.3, I should get 0.2 but I get 0 only. This makes no sense at all to me, it is probably a problem with using prompt but I need to use prompt or a method that is similar to prompt(I'm using sweetalert2 input for the alert). I am okay with using any js libraries.
const x = parseInt(prompt('1'))
const y = parseInt(prompt('2'))
alert(x-y)
I know it is a weird problem, but I don't know how to fix it.
You need to use parseFloat, not parseInt. parseInt is whole numbers only, while parseFloat allows decimal places.
parseFloat('0.9') === 0.9
parseInt('0.9') === 0

Round off Decimal Value in HTML [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Format number to always show 2 decimal places
(37 answers)
Closed 1 year ago.
I take data from Spreadsheet, So I use the below formula in App script
Now the data in spreadsheet value=2.33344443333
Hence in HTML same 2.33344443333 it's showing when i call emailTemp.data, Please help me to change to have only 2 decimal value like 2.33
Appscript:
data.forEach(function(row){
emailTemp.data=row[value];
}
.toFixed(2) would round a decimal value to two spaces as a string. Presumably the following would work if you didn't need to access the value as a decimal later on:
data.forEach(function(row){
emailTemp.data=row[value].toFixed(2);
}
Try this :
data.forEach(function(row){
emailTemp.data = Math.round(Number(row[value]) * 100) / 100;
}

Round Javascript Float number to 2 decimal digits [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 1 year ago.
In my project, when I am subtracting 96.74 from 60, javascript is giving me 36.739999999999995 , but I need 36.74 to do further calculation.
What should I do, how do I overcome this problem??
Thanks in advance.
Use parseFloat and set the decimal places to 2 with .toFixed(2)
console.log(parseFloat(36.739999999999995).toFixed(2))
If you want to get rid of trailing zeroes cast the result to a Number:
var num = Number(parseFloat(36.7).toFixed(2));
console.log(num);
Example how to round to two decimals, if that was what you wanted?
var x = 36.739999999999995;
x = Math.round(x*Math.pow(10,2))/Math.pow(10,2);
console.log(x);

How can I make this js code output to one decimal place? How do implement in MY SPECIFIC case? [duplicate]

This question already has answers here:
How do you round to 1 decimal place in Javascript?
(24 answers)
Closed 4 years ago.
This code outputs a number with many decimal places.. I'd like it limited to just one... this code gets me my desired result BUT with too many characters displayed.
I'm a total noob....
function oninputchange() {
$output.textContent = $input.value.split(/\s/g).map(syllable).reduce(sum)/4.7;
}
The results are expected... Just want fewer decimal places
Use method toFixed() for limit the decimal number to show

Format money value into pounds and pence [duplicate]

This question already has answers here:
How to format numbers as currency strings
(67 answers)
Closed 6 years ago.
I have helper function formatting my money value into pounds and pence:
formatMoney: function(10496.470000000001){
return value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "1,");
},
Gives me:
11,496.471,001,001,001
Is there a way I can format this accurately into pounds and pence? so it reads, £11,496.471p
In addition to the toFixed method call mentioned previously, the second parameter to replace should be "$1," rather than "1,". Also, your conversion to a number is somewhat fragile. Here's my attempt at remedying these issues:
function convert(value){
return "£"+((Number(value)||0).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"))+"p";
}
console.log(convert(10496.470000000001));
The Regexp doesn't care about the decimals you pass in. You need to convert your input to already have 2 decimals:
return value.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "1,");
^^^^^^^^^^
function convert(value){
return value.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "1,");
}
console.log(convert(10496.470000000001));

Categories