weird math calculation [duplicate] - javascript

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)

Related

JavaScript round up without decimal

I have a code in JavaScript
shipingcostnumber * parseInt(tax) / 100 + shipingcostnumber
returns number as 6655.866558 so I cover it in parentheses like:
(shipingcostnumber * parseInt(tax) / 100 + shipingcostnumber)
now it returns like 73213.8 which is correct number.
I need this number to be round up to 73214 and without any decimal.
So I made :
var nf = new Intl.NumberFormat('en-US', {
maximumFractionDigits:0,
minimumFractionDigits:0
});
and changed my code to:
nf.format(shipingcostnumber * parseInt(tax) / 100 + shipingcostnumber)
and it returns 73,214. It did round up my number but also added , how do I remove that decimal?
You don't need to write your own function. Just use Math.ceil(n). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil

Rounding a Number variable in javascript [duplicate]

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

when generating normally-distributed random values, what is the most efficient way to define the range?

FYI: random == pseudo-random
A. when generating uniformly-random numbers, I can specify a range, i.e.:
(Math.random()-Math.random())*10+5
//generates numbers between -5 and 15
B. generating a set of random values with a version of Gaussian-esque normal randomness:
//pass in the mean and standard deviation
function randomNorm(mean, stdev) {
return Math.round((Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1))*stdev+mean);
}
//using the following values:
{
mean:400,
standard_deviation:1
//results in a range of 397-403, or +-range of 3
},
{
mean:400,
standard_deviation:10
//results in a range of 372-429, or +-range of 30
},
{
mean:400,
standard_deviation:25
//results in a range of 326-471, or +-range of 75
}
each one gives me a range of approximately standard_deviation*(+-3) (assuming I left the program running longer).
C. I can calculate this range as follows:
assuming I want a range from 300-500, so var total_range = 200;
my mean is 400, my +-range is total_range/2 (var r = 100)
so standard_deviation would be r/3 or in this case 33.333.
This seems to be working, but I have no idea what I'm doing with math so I feel like an idiot, this solution feels kludgy and not totally accurate.
My question:
is there some formula that I'm dancing around that can help me here? my requirements are as follows:
must be able to define a range of numbers accurately.
must be done in JavaScript, as efficiently as possible.
I think maybe I'm close but it's not quite there.
Subtracting two random numbers doesn't give you a normal distribution, it will give you numbers that decline linearly on both sides of zero. See the red diagram in this fiddle:
http://jsfiddle.net/Guffa/tvt5K/
To get a good approximation of normal distribution, add six random numbers together. See the green diagram in the fiddle.
So, to get normally distributed random numbers, use:
((Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random()) - 3) / 3
This method is based on the central limit theorem, outlined as the second method here: http://en.wikipedia.org/wiki/Normal_distribution#Generating_values_from_normal_distribution
I wanted to have gaussian random numbers between 0 and 1, and after many tests (thanks to #Guffa answer too) I found this to be the best:
function gaussianRand() {
var rand = 0;
for (var i = 0; i < 6; i += 1) {
rand += Math.random();
}
return rand / 6;
}
And as a bonus:
function gaussianRandom(start, end) {
return Math.floor(start + gaussianRand() * (end - start + 1));
}

Rounding a number up [duplicate]

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

how to get 1.450 = 1.5 in javascript? (round to 1 decimal place) [duplicate]

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);
}

Categories