# 数学计算 - 浮点数[重复](Math Calculation - float numbers [duplicate])

```
这个问题在这里已有答案：

除法结果始终为零[重复] 4个答案

我正在做以下事情
```float years = (1/31536000) * 883102.00;
```
我得到年= 0000000
而实际答案是0.0.28
什么可能出错？

Division result is always zero [duplicate]  4 answers

I am doing the following
```float years = (1/31536000) * 883102.00;
```
and I get years = 0000000
while the actual answer is 0.0.28
Any suggestion on what might be going wrong ?原文：https://stackoverflow.com/questions/18108769```

## 最满意答案

``` 你应该这样做
```float years = (1.0/31536000.0) * 883102.00;
```
要么
```float years = (1.0/31536000) * 883102.00;
```
可能也会有效。 您的第一个数字被视为整数。 You should instead do
```float years = (1.0/31536000.0) * 883102.00;
```
or
```float years = (1.0/31536000) * 883102.00;
```
might work as well. Your very first number is treated as integer.```
2013-08-07

## 使用html表单将Javascript减法并乘以两位小数(Javascript subtraction and multiplication to two decimal places using html forms)

var answer = Number(currentprice) - Number(ourprice); 由于currentprice和ourprice是字符串，因此您需要将其解析为int或float或Number 。 您可以根据优先级使用parseInt或parseFloat函数。 如果您想要修复两个小数位，请尝试以下代码。 var answer = (Number(currentprice) - Number(ourprice)).toFixed(2); var annualvolu