简体   繁体   English

对于大而准确的数字,我应该使用哪种数据类型?

[英]What data type should I use for large and accurate numbers?

I'm making a simple Android calculator app which calculates percentages based on the numbers the users input. 我正在制作一个简单的Android计算器应用,该应用会根据用户输入的数字来计算百分比。 Usually it's within a reasonable value, so I used int. 通常它在合理的值范围内,所以我使用了int。 But I ran into problems with big numbers, and int automatically disposes of decimal values, so I'm trying to find an alternative. 但是我遇到了大数字的问题,并且int自动处理十进制值,因此我试图找到一个替代方法。 What data type should I use for big numbers, that is very accurate? 大数字应该使用哪种数据类型,这是非常准确的?

What data type should I use for large and accurate numbers? 对于大而准确的数字,我应该使用哪种数据类型?

I would use a BigDecimal per the Javadoc it is 我会根据每个Javadoc使用BigDecimal

Immutable, arbitrary-precision signed decimal numbers. 不变的,任意精度的带符号十进制数字。 A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale . BigDecimal由任意精度的整数未缩放值和32位整数scale组成

For example, 例如,

BigDecimal a = new BigDecimal("1000");
BigDecimal b = new BigDecimal("0.001");
BigDecimal c = a.multiply(b);
System.out.println(c.toPlainString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM