如何使用 JavaScript 将小数点按点分割成整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9098776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:35:21  来源:igfitidea点击:

How to split a decimal by its dot into integers using JavaScript?

javascriptsplit

提问by Mathlight

I have the following code;

我有以下代码;

var looptijd_1 = de_snelheid * de_afstand;
var split_looptijden_1 = new Array();
split_looptijden_1 = looptijd_1.split('.');

As you can see, is the idea very simple. I want to split an number by its dot. So if the number is 69.07, i want an variable with 69and an variable with 07.

如您所见,这个想法非常简单。我想用点分割一个数字。所以如果数字是69.07,我想要一个变量69和一个变量07

I know I have to make a string of the variable and split them, but I need the 07as 07and not 7(because I've got the do some math with it).

我知道我必须创建一个变量字符串并将它们拆分,但我需要07as07而不是7(因为我已经用它做一些数学运算)。

I know that I can take the first value, and store that, then the variable - the stored variable, so I've got 0.07, but I hope there's an better way tho achieve this, because this needs to be done 12 times on the page.

我知道我可以取第一个值,然后存储它,然后存储变量 - 存储的变量,所以我得到了0.07,但我希望有更好的方法来实现这一点,因为这需要在页面上完成 12 次.

Is there a better way to achieve my goal (variable1 = 69; variable2=07) and both integers?

有没有更好的方法来实现我的目标 ( variable1 = 69; variable2=07) 和两个整数?

EDIT; The whole point, is that this is math about walking time... And there are none constant variables, so i lop them, and then i would like to do the trick (numbers after the dot * 60)

编辑; 重点是,这是关于步行时间的数学......而且没有常数变量,所以我把它们扔掉,然后我想做这个把戏(点* 60后的数字)

回答by LukeH

If you're wanting to perform calculations with the results then there's no need to convert your number to a string, split it, and then convert the resulting strings back to numbers. Use some basic maths instead:

如果您想对结果进行计算,则无需将您的数字转换为字符串,将其拆分,然后将结果字符串转换回数字。使用一些基本的数学来代替:

var split_looptijden_1 =
    [
        (looptijd_1 > 0) ? Math.floor(looptijd_1) : Math.ceil(looptijd_1),
        looptijd_1 % 1
    ];

回答by user2700307

This is my assumption:

Convert your decimal number into string and then split it using .split() method then store the result into an array.

var num=10.07;
var str=num.toString();
var numarray=str.split('.');
var a=new Array();
a=numarray;
console.log(a[0]);
console.log(a[1]);

try with the below example, you will get your desired result.If console.log() not worked

试试下面的例子,你会得到你想要的结果。如果 console.log() 不起作用

its working fine for me.

Demo:

<!DOCTYPE html>
<html>
<body>

<p id="demo">Integer Part</p>
<p id="demo1">Decimal Part</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{

var num=10.07;
var str=num.toString();
var numarray=str.split('.');
var a=new Array();
a=numarray;


var x = document.getElementById("demo");
var y = document.getElementById("demo1");
x.innerHTML=a[0];
y.innerHTML=a[1];
}demo1
</script>

</body>
</html>

回答by lonesomeday

Make the number a string (it's a number at the moment, which can't be split), then split it:

使数字成为字符串(目前是数字,不能是split),然后将其拆分:

var split_looptijden_1 = looptijd_1.toString().split('.');
console.log(split_looptijden_1[0]); // 69
console.log(split_looptijden_1[1]); // 07

NB that the new Array()is not necessary. splitreturns a new array anyway; the one you created is overwritten. Since Javascript uses static typing, you don't need to declare a variable's type. Note also that, if you want integers, you'll have to forget leading zeros. If you want a particular way of formatting characters, that's called a string.

请注意,new Array()没有必要。split无论如何返回一个新数组;您创建的那个被覆盖了。由于 Javascript 使用静态类型,因此您不需要声明变量的类型。另请注意,如果您想要整数,则必须忘记前导零。如果你想要一种特殊的格式化字符的方式,那就叫做字符串。

回答by Amit

This should do what u need

这应该做你需要的

var looptijd_1 = de_snelheid * de_afstand;
var split_looptijden_1 = new Array();
split_looptijden_1 = (looptijd_1+"").split('.');

回答by Fosco

You must convert the number to a string first:

您必须先将数字转换为字符串:

split_looptijden_1 = looptijd_1.toString().split('.');

回答by Diodeus - James MacFarlane

You don't need to make an array. Split does this automatically.

您不需要创建数组。拆分会自动执行此操作。

var looptijd_1 = de_snelheid * de_afstand + "";
var split_looptijden_1 = looptijd_1.split('.');

回答by Devin Burke

An integer can't be stored with leading zeros.

整数不能与前导零一起存储。

Try:

尝试:

var looptijd_1 = de_snelheid * de_afstand;
var split_looptijden_1 = looptijd_1.toString().split('.');

This will convert the integer values to strings, thereby storing the 07rather than 7.

这会将整数值转换为字符串,从而存储07而不是7.

回答by klaus thorn

Pros:

优点:

  • This solution does not use the "first value" (67) when calculating the "second" (0.07). (Words the original poster used, in " ")

  • It also avoids storing 0.06999999999999318 instead of 0.07 by using the factor 1000. Increase the factor to e.g. 10000 to handle e.g. 0.007. (*10 for each additional digit)

  • Works as expected for negative values. Does no rounding or conversions.

  • 此解决方案在计算“第二个”(0.07) 时不使用“第一个值”(67)。(原海报使用的话,在“ ”中)

  • 它还通过使用因子 1000 避免存储 0.06999999999999318 而不是 0.07。将因子增加到例如 10000 以处理例如 0.007。(*每增加一位数字 10)

  • 对于负值,按预期工作。不进行四舍五入或转换。

Note: This solution ignores the request for "07", which was just part of a suboptimal solution mixed into the answer.

注意:此解决方案忽略了对“07”的请求,它只是混入答案的次优解决方案的一部分。

variable1 = looptijd_1|0;                // = 69 as number. No rounding.
variable2 = (looptijd_1*1000%1000)/1000; // = 0.07 as number. Simple form: x % 1 

Apart from the solution:

除了解决方案:

You said that you need to do this 12 times in one page. But you did not tell whether your concern is the time needed to calculate or cluttered code. Or both.

你说你需要在一页上做 12 次。但是您没有说明您关心的是计算所需的时间还是杂乱的代码。或两者。

Time can be ignored for 12 times these simple calculations.

时间可以忽略12次这些简单的计算。