How can i substract the result i get from x without loosing a decimal place in an html input box??
So, lets say i have x=40.40 and i substract 25, the result should be 15.40 and i am getting 15.4 without that zero. How can i prevent loosing that extra decimal, if i necesarily want to substract that 25 without altering anything else?? (For y i get a 0 with no decimal places and i need 0.00)
document.getElementById("posx").value=(x.toFixed(2)-25.00);
document.getElementById("posy").value=(y1.toFixed(2)-(-2.45));
You'll have to toFixed it after the subtraction
document.getElementById("posx").value = (x - 25.00).toFixed(2);
toFixed converts the value to a string, as numbers don't have zero padding, only the decimals needed to correctly reflect the value of the number.
When you subtract a number from a string, you end up with a number, and as numbers only have the needed decimals, no zero padding, you get 15.4, and once again have to use toFixed to convert it to a string with the decimals you need.
Related
Dos it exist a maximum float number with two digits after decimal point that can be parsed with JSON.parse without losing precision?
For example:
JSON.parse('{"amount": 9999999999999.99}')
{amount: 9999999999999.99} // not lose (probably)
JSON.parse('{"amount": 99999999999999.99}')
{amount: 99999999999999.98} // lose
If x is a decimal floating-point number with 15 significant digits or fewer, then converting x to JavaScript’s Number type and then converting the result back to a decimal floating-point number with the same number of significant digits produces exactly x, provided the number is within normal bounds. Therefore, all decimal numerals with two digits after the decimal point from “.00” to “9999999999999.99” can be parsed, stored, and reformatted with two digits after the decimal point, and the result will be the original numeral.
The stored value will generally not equal the original value. For example, when “.99” is parsed, the result will be exactly 0.9899999999999999911182158029987476766109466552734375. However, the stored value will be sufficiently close to the original value that, when converted back to the original number of digits, the original value is recovered. Note that you must know the original number of digits; it is not inherently a part of the Number value.
15 is a lower bound for this property. There may be some exponent values for which all 16-digit decimal numerals survive a round trip. However, since 99999999999999.99 (16 digits) produces 99999999999999.98, we know this is not one of those intervals.
If you want to know the specific number between 9999999999999.99 and 99999999999999.99 where this round-trip property first fails, you may have to hunt for it computationally. It many not be a value that is easy to calculate directly by mathematical properties.
How to keep '.00' in Javascript? I currently have the string "123456789012.00";
I want to get the double 123456789012.00 to keep .00
toFixed(2) will return a string
parseFloat() will cast the .00
How can I do this?
A float uses the precision it needs (that's why it's called a "float" -- as in "floating point", the point has no fixed position).
If you want to display a float with the 2 significant digits (i.e. 2 digits after the point), you can use toFixed(2). That will not change the number, but will store it in a string with the number of digits you want to show.
You can use the toFixed() method to do this. The code below will log the result you want in the console of your browser.
var num = "123456789012.00";
console.log(parseFloat(num).toFixed(2));
I have a simple conversion form from kg to lbs.
html
<input type="text" id="kg" name="kg">
<input type="text" id="lbs" name="lbs">
I have it setup so that the lbs box updates while you type in the kg box with this code.
jQuery
$("#kg").keyup(function(){
$('#lbs').val($('#kg').val()*2.20462);
});
How do I get the lbs value to round to 2 decimals places? I am sure it is something fairly simple but all the examples I found online are for if the number is stored in a variable.
Use toFixed
var string = yourNumber.toFixed(2);
use toFixed:
$('#lbs').val(($('#kg').val()*2.20462).toFixed(2));
number.toFixed( [digits] )
Parameter
digits The number of digits to appear after the decimal point; this
may be a value between 0 and 20, inclusive, and implementations may
optionally support a larger range of values. If this argument is
omitted, it is treated as 0.
Returns
A string representation of number that does not use exponential
notation and has exactly digits digits after the decimal place. The
number is rounded if necessary, and the fractional part is padded with
zeros if necessary so that it has the specified length. If number is
greater than 1e+21, this method simply calls Number.toString() and
returns a string in exponential notation.
also this
(10.8).toFixed(2); // 10.80
var num = 2.4;
alert(num.toFixed(2)); // 2.40
Formatting a number with exactly two decimals in JavaScript
I have a number with a comma, for example: 254,5. I need the 0 behind the ,5 so it stands like 254,50 instead..
I'm using this to get the number:
Math.floor(iAlt / 50) * 50;
How can i get the 0 behind the ,5?
Try the toFixed() method, which pads the decimal value to length n with 0's.
var result = (Math.floor(iAlt / 50) * 50).toFixed(2);
A Number will always remove trailing zeros, so toFixed returns a String.
It's important to note that toFixed must be called on a number. Call parseFloat() or parseInt() to convert a string to a number first, if required (not in this situation, but for future reference).
I set up a system that parses a compact data string into JSON. I'm using a 19 digit number to store ids. Unfortunately any number greater than 17 digits, parseFloat() rounds the last few digits.
This breaks the whole data string. Can I fix this?
For example 8246295522085275215 gets turned into 8246295522085276000. Why is this?
http://jsfiddle.net/RobertWHurst/mhZ7Q/
JavaScript has only one numeric type, which is an IEEE 754 double precision floating-point. That means, you have a maximum of 52 bits of precision, which is a bit more than 15 decimal places.
If you need more precision than that, you have to use a bignum library or work with strings.
Numbers in JavaScript lose precision if they are higher than a certain value.
According to http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference, integers are only reliable up to 15 digits (9 * 10^15 to be exact).
Try one of these
1. Use a string
2. Split your number in two and save the smaller parts to an array
3. Bignum library
4. Use a smaller number if you can