[英]float value extraction in javascript
how can i extract float value like (0.9) to be a whole integer value (9) in JavaScript? 如何在JavaScript中提取像(0.9)这样的浮点值成为一个整数(9)?
example 例
var total = 67.9;
something to process var whole var DEC to be like
var whole = 67;
var DEC = 9;
The simplest way I can think of is this: 我能想到的最简单的方法是:
var string = total.toString();
var dotidx = string.indexOf(".");
var dec = dotidx >= 0 ? +string.slice(dotidx + 1) : 0;
Notice that it doesn't work with numbers in exponential forms. 请注意,它不适用于指数形式的数字。
There are many ways to find a result. 有很多找到结果的方法。 I give you one simple solution.
我给你一个简单的解决方案。
var total = 67.9
var splitVal = total.toString().split(".")
var whole = splitVal[0], var desc = splitVal[1] || 0
and if you want to convert whole and desc in number then multiply by 1. 如果要转换整数和desc的数字,请乘以1。
var whole = splitVal[0] * 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.