round 2 decimals syntax from from input using jQuery - javascript

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

Related

.tofixed(2) not working when substracting a result?

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.

Format a number with an exponent in javascript?

I have a number which is displaying as follows:
1.0333333333333335e-9
I'd like to limit the number of digits that's shown, so it looks more like:
1.03e-9
How do I do this?
numObj.toExponential(fractionDigits) has it built in
fractionDigits
An integer specifying the number of digits after the decimal point.
Defaults to as many digits as necessary to specify the number.

Regular expression to enforce 2 digits after decimal point

I need to validate a numeric string with JavaScript, to ensure the number has exactly two decimal places.
The validation will pass only if
the number has precisely two decimal places
there is at least one digit before the decimal point. (could be zero)
the number before the decimal point can not begin with more than one zero.
Valid numbers:
0.01
0.12
111.23
1234.56
012345.67
123.00
0.00
Invalid numbers:
.12
1.1
0.0
00.00
1234.
1234.567
1234
00123.45
abcd.12
12a4.56
1234.5A
I have tried the regular expression [0-9][\.][0-9][0-9]$, but it allows letters before decimal point like 12a4.56.
. matches any character, it does not do what you think it does. You have to escape it. Also, you have two more errors; try
^[0-9]+\.[0-9][0-9]$
instead, or even better, use \d for decimal digits:
^\d+\.\d\d$
This covers all requirements:
^(0|0?[1-9]\d*)\.\d\d$
the number has precisely two decimal places
Trivially satisfied due to the non-optional \.\d\d$
The other two conditions can be restated as follows:
The number before the decimal points is either a zero
or a number with exactly one zero, then a number that does not start with zero
This is covered in these two cases:
0
0?[1-9]\d*
You don't need regular expressions for this.
JavaScript has a function toFixed() that will do what you need.
var fixedtotwodecimals = floatvalue.toFixed(2);
i used this
^[1-9][1-9]*[.]?[1-9]{0,2}$
0 not accept
123.12 accept but 123.123 not accept
1 accept
12213123 accept
sdfsf not accept
15.12 accept
15#12 not accept
15&12 not accept
var values='0.12';
document.write(values.match(/\d+[.]+\d+\d/));
change value as you want and check it
Here it is:
^(0[.]+\d{2})|^[1-9]\d+[.]+\d{2}$
Try This Code
pattern="[0-9]*(\.?[0-9]{1,2}$)?"
1 Valid
1.1 Valid
1.12 Valid
1.123 not Valid
only number Valid
pattern="[0-9]*(.?[0-9]{2}$)?"
1 Valid
1.1 not Valid
1.12 Valid
1.123 not Valid
only number Valid

Parse Float has a rounding limit? How can I fix this?

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

Rounding the result of division in Javascript

I'm performing the following operation in Javascript:
0.0030 / 0.031
How can I round the result to an arbitrary number of places? What's the maximum number that a var will hold?
Modern browsers should support a method called toFixed(). Here's an example taken from the web:
// Example: toFixed(2) when the number has no decimal places
// It will add trailing zeros
var num = 10;
var result = num.toFixed(2); // result will equal 10.00
// Example: toFixed(3) when the number has decimal places
// It will round to the thousandths place
num = 930.9805;
result = num.toFixed(3); // result will equal 930.981
toPrecision() might also be useful for you, there is another excellent example on that page.
For older browsers, you can achieve it manually using Math.round. Math.round() will round to the nearest integer. In order to achieve decimal precision, you need to manipulate your numbers a bit:
Multiply the original number by 10^x
(10 to the power of x), where x is
the number of decimal places you
want.
Apply Math.round()
Divide by 10^x
So to round 5.11111111 to three decimal places, you would do this:
var result=Math.round(5.111111*1000)/1000 //returns 5.111
The largest positive finite value of the number type is approximately 1.7976931348623157 * 10308. ECMAScript-262 3rd ed. also defines Number.MAX_VALUE which holds that value.
To answer Jag's questions:
Use the toFixed() method. Beware; it returns a string, not a number.
Fifteen, maybe sixteen. If you try to get more, the extra digits will be either zeros or garbage. Try formatting something like 1/3 to see what I mean.

Categories