[英]How to round returned value from date.getTime() method
I am trying to round the following value 1.48824E9
, that is returning inside the date.getTime() method. 我试图舍入以下值
1.48824E9
,该值在date.getTime()方法内部返回。
This is what I've tried but It's not working: 这是我尝试过的方法,但是不起作用:
private double x;
private double y;
DecimalFormat df;
private double z;
private String g;
double a;
public GraphPoints(Date x, double y) {
df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.CEILING);
a = x.getTime();
g = df.format(a);
z = Double.parseDouble(g);
System.out.println("THIS IS THE ROUNDED VALUE: " + z);
this.x = z;
this.x = z;
this.y = y;
}
I'm trying to round it to one decimal place. 我正在尝试将其四舍五入到小数点后一位。 Could someone help me please?
有人可以帮我吗?
You don't have any decimal places in your number (I mean you have 2 decimal places by default .00
, but I assume that it is not what you are looking for). 您的数字中没有小数位(我的意思是默认情况下您有2个小数位
.00
,但我认为这不是您要查找的)。 "1.48824E9"
is a scientific notation for the "1488240000.00" double. "1.48824E9"
是“ 1488240000.00”双精度符号的科学表示法。 Consider this code snippet: 考虑以下代码片段:
Double d = Double.parseDouble("1.48824E9");
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(d));
System.out.println(d);
Output: 输出:
1488240000.00
1.48824E9
Not sure though how you get it because date.getTime()
returns long and Long.toString()
doesn't convert to scientific notation. 不知道如何获取它,因为
date.getTime()
返回long而Long.toString()
不会转换为科学计数法。 So, if you want to print "1.5"
from "1.48824E9"
you can do it like this for example: 因此,如果要从
"1.48824E9"
打印"1.5"
,则可以这样操作:
Double d = Double.parseDouble("1.48824E9");
d = d / Math.pow(10, 9);
NumberFormat formatter = new DecimalFormat("#0.0");
System.out.println(formatter.format(d));
Output: 输出:
1.5
If you actually want to change the value of your double you can do it this way: 如果您实际上要更改double的值,则可以通过以下方式进行操作:
Double d = Double.parseDouble("1.48824E9");
d = d / Math.pow(10, 9);
d = Math.round (d * 10.0) / 10.0;
System.out.println(d);
Output: 输出:
1.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.