Suppose I have to access a some pattern of digit using javascript. Please have a look.
For Example :: Pattern needed :: Fomat - $12.00 only
If user enters the value 123.12 => Output = $123.12
If user enters the value 开发者_如何学编程123.1 => Output = $123.10
If user enters the value 123.1237 => Output = $123.12 :: Here I am unable to get the first two element of second array having value as 1237.
Thanks
Why not:
var number = 123.1237;
var dollarAmount = number.toFixed(2);
console.log(dollarAmount);
Nasty string manipulation is not the answer here.
If you have '123.1237
as a string input
, then input.split('.')
would give you an array with two strings 123
and 1237
. Thus what you really want to do is sub-string the second string to get the first two characters:
1237
.subsring(0, 2);
精彩评论