开发者

Access First Two Values of any Array made by Split?

开发者 https://www.devze.com 2023-01-26 04:52 出处:网络
Suppose I have to access a some pattern of digit using javascript. Please have a look. For Example :: Pattern needed :: Fomat - $12.00 only

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

0

精彩评论

暂无评论...
验证码 换一张
取 消