You can use toFixed() to do that
var twoPlacedFloat = parseFloat(yourString).toFixed(2)
Answer from Mahesh Velaga on Stack OverflowYou can use toFixed() to do that
var twoPlacedFloat = parseFloat(yourString).toFixed(2)
If you need performance (like in games):
Math.round(number * 100) / 100
It's about 100 times as fast as parseFloat(number.toFixed(2))
http://jsperf.com/parsefloat-tofixed-vs-math-round
EDIT:
https://jsperf.app/kowulu
Videos
Number.prototype.toFixed() returns a string representation of the number you call it on with the right number of decimals, but if you parse it back to number with parseFloat, those will go away, as number doesn't care about trailing zeros.
You can fix that by just getting rid of the parseFloat:
const tips = [];
function calculateTip(bill) {
if (bill < 50)
tips.push((bill * 0.2).toFixed(2));
else if (bill >= 50 && bill < 201)
tips.push((bill * 0.15).toFixed(2));
else
tips.push((bill * 0.10).toFixed(2));
}
calculateTip(124);
calculateTip(48);
calculateTip(268);
console.log(tips);
Numbers in Javascript are output to console with as many decimal places as needed.
console.log(1.0000001)
console.log(1.1)
All your numbers only display with one decimal point as they only have one decimal place. You should return a string instead if you want to show them with a specific precision.
var tips = [];
var calculateTip = function(bill) {
switch (true) {
case bill < 50:
tips.push((bill * 0.2).toFixed(2));
break;
case bill >= 50 && bill < 201:
tips.push((bill * 0.15).toFixed(2));
break;
default:
tips.push((bill * 0.10).toFixed(2));
}
}
calculateTip(124);
calculateTip(48);
calculateTip(268);
console.log(tips);
Javascript has a type string and a type number, but does not have separate int and float types.
You can start with some strings:
> var intStr = "446";
> var floatStr = "446.0";
> var decStr = "446.";
> var floatVal = "446.7";
All of these have the type string
> typeof floatStr
"string"
Once you parse them using the Number object the result is of type number
> var n = Number.parseInt(intStr);
> n
446
> typeof n
"number"
> var f = Number.parseFloat(floatVal);
> f
446.7
> typeof f
"number"
So both parseInt and parseFloat produce a number.
Parsing a string with a zero 0 fractional value drops the fractional part, because 446.0 == 446
> Number.parseFloat(floatStr);
446
Following that, parsing a string that has a trailing decimal with no value is the same as a .0 value. 446.0 == 446. == 446
> Number.parseFloat(decStr);
446
If you want to keep the trailing decimal . you will have to maintain the variable as a string, and only convert it to a number when you are ready to perform computations with its value.
This is because (using your example) 44. is interpreted by Javascript as 44.0 which is the same as 44, which is what it then converts it to.
In JS, you do not have multiple data types for numbers - there are no ints, floats, etc, but instead there is number, a data type used to represent all numbers that can be interacted with in code. The difference between parseInt and parseFloat then is that parseInt strips the decimal values and parseFloat does not, however they both still return a number.
If I were making a calculator as you are, I would wait until the user had entered their full number before using parseInt or parseFloat on it. Assuming that the user is inputting into some sort of text input or DOM container, it will be a string anyway in the DOM, so there is no need to convert it to a number. Once you do come to processing the calculations, it won't matter if 44. is interpreted as 44, because that is in fact what it is.
You are probably receiving 446 rather than 44.6 because the decimal is not being inserted (parseFloat of decimal with nothing after it removes decimal, meaning the user is actually typing 446). Leave it as a string until the user finishes typing, then convert and the problem will be solved.
It might be worth investigating a library like decimal.js to help with this. e.g.:
Decimal.set({ precision: 24 }); // The default of 20 is not big enough for the number below so you may need to adjust this
let a = new Decimal('2019275.159999999912345');
a = a.plus(1);
let b = new Decimal('2019275.159999999912345').plus(1);
let c = Decimal.add('2019275.159999999912345', 1);
console.log(a, b, c);
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/9.0.0/decimal.min.js"></script>
You can use toFixed() method after parseFloat() otherwise it will return least precision equivalent to internal representation
parseFloat("2019275.159999999916180968").toFixed(18)