eclipse 打印小数点前的位数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9891211/
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-09-19 17:55:11  来源:igfitidea点击:

Print the number of digits before a decimal point

javaeclipse

提问by Wormhole99

I know there are ways to get the number of digits after a decimal point, for instance the substring method, but how would I go about doing this for the number of digits before a decimal place?

我知道有一些方法可以获取小数点后的位数,例如 substring 方法,但是我将如何针对小数点前的位数执行此操作?

I need to use this to convert US change (double) into Euro change(double). The way I would like to do this is by taking the number before a decimal (such as $1.) and times it by its euro equivalent (.7498) and take the number after a decimal (.16) and times that by its .01 euro coin value (.0075), add both values together to get the euro equivalent of $1.16 (.8698).

我需要使用它来将美国零钱(双)转换为欧元零钱(双)。我想这样做的方法是取小数点前的数字(例如 1 美元),然后乘以欧元等价物 (.7498),然后取小数点 (.16) 后的数,乘以 . 01 欧元硬币价值 (.0075),将这两个价值相加得到相当于 1.16 美元 (.8698) 的欧元。

回答by Android Killer

To get the number before decimal point,do this:

要获取小数点前的数字,请执行以下操作:

String str = new Double(your_double_number).toString().subString(0,str.indexOf('.'));
double v = Double.valueOf(str);

If you are using '$' sign then take 1 in place of 0.

如果您使用“$”符号,则取 1 代替 0。

Hope it will help you.

希望它会帮助你。

回答by Michael Borgwardt

convert US change (double) into Euro change(double)

将美元找零(双)转换为欧元找零(双)

Please don't do that. Never, never, everuse doubleor floatto represent money, because those datatypes cannot represent most decimal fractions, so you get rounding errors before you even start to do any calculations.

请不要那样做。永远,永远,永远不要使用doublefloat代表货币,因为这些数据类型不能代表大多数小数,所以在你开始做任何计算之前你就会得到四舍五入的错误。

Instead, use BigDecimal.

相反,使用BigDecimal.

回答by Matt Fellows

First of all - just multiplying the Dollar value by the exchange rate will get you the euro value so there's no need to do that as far as i can see - you will just introduce rounding errors.

首先 - 只需将美元价值乘以汇率就会得到欧元价值,所以据我所知,没有必要这样做 - 你只会引入舍入误差。

But if you did need to - just use substring

但是,如果您确实需要 - 只需使用子字符串

String dollarVal = ".16"
String justFullDollar = dollarVal.substring(1, dollarVal.indexOf("."));
String justCents = dollarVal.substring(dollarVal.indexOf(".")+1);

The Correct way would be to store all you money as integers or arbitrary precision objects that way you get no floating point errors too.

正确的方法是将您所有的钱存储为整数或任意精度的对象,这样您也不会出现浮点错误。

Convert to cents, multiply and convert back again. e.g.

转换为美分,乘以再转换回来。例如

String dollarVal = ".16"
BigDecimal dollars = new BigDecimal(dollarVal.substring(1)); //1.16
BigDecimal cents = dollars.multiply(new BigDecimal(100)); //116
BigDecimal eurocents = cents.multiply(new BigDecimal(exchangeRate)); //86.9768
BigDecimal euros = eurocents.divide(new BigDecimal(100)); //0.869768
DecimalFormat formatter = new DecimalFormat("###.00");
String euroVal = "" + formatter.format(euros);

回答by Chandra Sekhar

You can use

您可以使用

String s[] = new Double(your number).toString().split(".");

The s[0]is the number before decimal point. and s[1]is the number after decimal point.

S [0]是小数点之前的数目。和S [1]是小数点后的位数。

Both are in String, so you need to parse them into double using

两者都在字符串中,因此您需要使用

double num1 = Double.parseDouble(s[0]);
double num2 = Double.parseDouble(s[1]);