简体   繁体   English

Android将double乘以int [java]

[英]Android multiplying double with int [java]

I have a double value a = 0.00059 and an Integer value which gets incremented and multiplied with the double value (say b = 1) 我有一个double值a = 0.00059和一个Integer值,该值递增并乘以double值(例如b = 1)

when I set the answer to the textview 当我为textview设置答案时

//for b = 1
view.setText(((double)(a*b)));

the answer I get is " 5.9E-4 " however it should be 0.00059. 我得到的答案是“ 5.9E-4”,但是应该是0.00059。

am I multiplying the values correctly.? 我可以正确乘以值吗?

In addition to the other answers provided, you can use a formatter: 除了提供的其他答案外,您还可以使用格式化程序:

NumberFormat formatter = new DecimalFormat("#.#####");
view.setText(formatter.format(a*b));

You are multiplying them correctly. 您正确地乘以它们。 The values 5.9E-4 and 0.00059 are equivalent, mathematically and programmatically. 5.9E-40.00059在数学和程序上均等效。 The representation 5.9E-4 is like scientific notation, ie 5.9x10^(-4) is equivalent to 0.00059. 5.9E-4表示就像科学记数法,即5.9x10 ^(-4)等于0.00059。

You get the same value as you want to get, but formatted in a scientific notation. 您将获得与想要获得的值相同的值,但格式必须科学。 What you need to do is to explicitly convert it to String: 您需要做的是将其显式转换为String:

view.setText(String.format("%f", a*b));

And you could eventually specify the number of decimal places to print after the decimal separator in this way: 您最终可以通过这种方式指定要在小数点分隔符之后打印的小数位数:

// displays two digits after the decimal separator
view.setText(String.format("%.2f", a*b));  

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

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