This question already has answers here:
Rounding numbers to 2 digits after comma
(10 answers)
Closed 9 years ago.
I want to round the up my total til two decimal places.
I have this:
var updateTotal = function () {
var people = parseInt($('#people').val());
var bill = parseInt($('#bill').val());
var tip = parseInt($('#tip').val());
var billTip = bill + tip;
$('#total').text(billTip / people);
and i've also found this snippet to help round up but i cant seem to get my head around how to implement it.
var rounded = Math.round((10 / 3) * 100) / 100;
Thanks
It's already implemented for you. Substitue (10 / 3) for your own variables. All it's doing is shifting the decimal place two places to the right (by multiplying by 100), rounding, then shifting it two left (by dividing by 100).
var rounded = Math.round((billTip / people) * 100) / 100;
You can also use .toFixed
$('#total').text((billTip / people).toFixed(2));
I would use parseFloat on both your numbers, or else it will round to 00:
$('#total').text(parseFloat(billTip / people).toFixed(2));
You can use ceil function Math.ceil(billTip)
and for refernce you can also visit below link
http://www.w3schools.com/jsref/jsref_ceil.asp
Related
This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 6 years ago.
I have this code:
function sellByte() {
if (player.bytes >= 1) {
player.bytes = player.bytes - 1;
player.money = player.money + 0.10;
document.getElementById("bytes").innerHTML = "Bytes: " + player.bytes;
document.getElementById("money").innerHTML = "$" + player.money;
}
}
And whenever I sell a Byte my money value ends up looking like $10.00000003 or something along those lines, how would I go about rounding the money value UP every time this function is run?
Working with float numbers in JS is very tricky. My suggestion is to operate only with smaller units (cents instead of dollars) and then you will only deal with integers and will not have similar issues.
Use Math.round(player.money* 100) / 100 for 2 decimal rounding.
Use any of the following code
Math.round(num * 100) / 100
using fixed Method
var numb = 123.23454;
numb = numb.toFixed(2);
or you can refer following link for more help
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
For example, I have a number 123.429. How can I remove the trailing decimals without rounding up to two decimal place.
Hence, I need the number to be up to two d.p. i.e 123.42.
Definitely toFixed() method or Math.round(num * 100) / 100 cannot be used in this situation.
The function you want is Math.floor(x) to remove decimals without rounding up (so floor(4.9) = 4).
var number = Math.floor(num * 100) / 100;
Edit: I want to update my answer because actually, this rounds down with negative numbers:
var Math.floor(-1.456 * 100) / 100;
-1.46
However, since Javascript 6, they have introduced the Math.trunc() function which truncates to an int without rounding, as expected. You can use it the same way as my proposed usage of Math.floor():
var number = Math.trunc(num * 100) / 100;
Alternatively, the parseInt() method proposed by awe works as well, although requires a string allocation.
var number = parseInt('' + (num * 100)) / 100;
You can convert it to a string and then simply truncate the string two places after the decimal, e.g.:
var s = String(123.429);
s.substring(0, s.indexOf('.') + 3); // "123.42"
Please note that there's no guarantee if you convert that final string back into a number that it'll be exactly representable to those two decimal places - computer floating point math doesn't work that way.
another v. cool solution is by using | operator
let num = 123.429 | 0
let num = 123.429 | 0
console.log(num);
let's get the variable name as "num"
var num = 123.429;
num=num*100;
num=num.toString();
num=num.split(".");
num=parseInt(num[0]);
num=num/100;
value of the num variable will be 12.42
Try this
number = parseFloat(number).toFixed(12);
number = number.substring(0, number.indexOf('.') + 3);
return parseFloat(number);
Not the fastest solution but the only one that handles an edge case like 0.0006*10000 = 5.999999999 properly, i.e. if you want to truncate to 4 decimal places and the value is exactly 0.0006, then using Math.trunc(0.0006 * (10 ** 4))/(10 ** 4) gives you 0.0005.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you round to 1 decimal place in Javascript?
My Value is 1.450 and I have to round it to 1 decimal place.
I want 1.450 = 1.5 in Javascript can any body fix this please.
You need this:
var mynum = 1.450,
rounded = Math.round(mynum * 10) / 10;
suppose you have
var original=28.453;
Then
var result=Math.round(original*10)/10 //returns 28.5
From http://www.javascriptkit.com/javatutors/round.shtml
You can also see How do you round to 1 decimal place in Javascript?
Given your fiddle, the simplest change would be:
result = sub.toFixed(1) + "M";
to:
result = Math.ceil(sub.toFixed(1)) + "M";
If you use Math.round then you will get 1 for 1.01, and not 1.0.
If you use toFixed you run into rounding issues.
If you want the best of both worlds combine the two:
(Math.round(1.01 * 10) / 10).toFixed(1)
You might want to create a function for this:
function roundedToFixed(_float, _digits){
var rounder = Math.pow(10, _digits);
return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is JavaScript's Math broken?
I have a javascript and html code here my code
and i get a weird value when calculate specific fields. Only one checkbox has a script right now (the background music addon) and only when i select that add on with 1 of any of the 3in reels i get 24.990000000000002 in the total price. it's so weird its only with those inputs...it should be just 24.99....all the other inputs work and give accurate totals but just the 3 3in fields give those numbers...through trial and error I have found that the problem lies here
regthreetot = parseFloat(regthree * 50);
regfourtot = parseFloat(regfour * 100);
regfivetot = parseFloat(regfive * 200);
regsixtot = parseFloat(regsix * 300);
regseventot = parseFloat(regseven * 400);
supthreetot = parseFloat(supthree * 50);
supfourtot = parseFloat(supfour * 100);
supfivetot = parseFloat(supfive * 200);
supsixtot = parseFloat(supsix * 300);
supseventot = parseFloat(supseven * 400);
sixthreetot = parseFloat(sixthree * 50);
sixfourtot = parseFloat(sixfour * 100);
sixfivetot = parseFloat(sixfive * 200);
sixsixtot = parseFloat(sixsix * 300);
sixseventot = parseFloat(sixseven * 400);
the regthree,supthree and sixthree values are all multiplied by 50...all the other values are multiplied by a value with 3 digits...if i change 50 to 100 it will give a normal answer...why does the number of digits matter here and what can i do to fix it?
It happens because of the way float arithmetic works. You can work around it by rounding to 2 decimal places (you won't be using fractions of a cent anyways): http://jsfiddle.net/vSsW9/1/
Well until you get it figured out you can use Math.Round to round to two decimals. Assume your resulting var is called result, do result=Math.round(result*100)/100
It's caused by float variables not being 100% accurate, to fix it to max 2 decimal points:
n.toFixed(2)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
round number in JavaScript to N decimal places
This may be easy for you guys,
My question is:
How can I control a decimal places in a floating point value.
Ex.: My result is returning 0.365999999999999; but I need to show just 4 decimal numbers.
Check the demo: Demo (I accept any others ways to calculate that)
Thanks!
You can use .toFixed
var number = 0.365999999999999;
var rounded = number.toFixed(4); // 0.3660
try this:
$("#test").keyup(function(){
var number = parseFloat($("#number").text());
var current = parseFloat($(this).val());
var total = number*current;
$("#result").val(total.toFixed(4));
});
$("#result").val(total.toFixed(4));
Javascript has a nice round function, but it only does integers so you have to multiply it by 10000 then divide the rounded result by 10000
http://www.javascriptkit.com/javatutors/round.shtml
The toFixed function always rounds up, but round will probably do what you want.
For proper rounding:
function roundNumber(number, digits) {
var multiple = Math.pow(10, digits);
var rndedNum = Math.round(number * multiple) / multiple;
return rndedNum;
}
For rounding up:
number.toFixed(4);
$("#test").keyup(function(){
var number = $("#number").text();
var current = $(this).val();
var total = parseFloat(number*current).toFixed(2);
$("#result").val(total);
});
Cast the variable to a float and then use the toFixed() method
If you follow the link below you can import the number_format php function to javascript. The function has been helping me for years now.
Here is the function signature :
function number_format (number, decimals, dec_point, thousands_sep)
http://phpjs.org/functions/number_format:481