java去掉小数点
❶ java中怎么去掉不必要的.0
packagetest;
importjava.util.Scanner;
publicclassTest3
{
publicstaticvoidmain(String[]args)
{
doublea;
Scannersc=newScanner(System.in);
System.out.println("请输入数字:");
a=sc.nextDouble();
for(doubleb=1;b<=9;b++)
{
doubleresult=a/b;
if(result%1==0)
{
System.out.println(a+"/"+b+"="+(int)(a/b)+" ");
}
else
{
System.out.println(a+"/"+b+"="+a/b+" ");
}
}
System.out.println("");
}
}
❷ 如何在Java程序的sql中去掉小数点后多余的0
在你的代码中找:select avg(money)
替换为:select cast(round(avg(money),2) as decimal(18,2))
❸ java从数据库调用的时间怎么去掉小数点的值
如果你是sql server数据库,你可以用CONVERT(VARCHAR(20),time,120)函数
SELECT CONVERT(VARCHAR(20),scheletime,120) from ......
❹ java Date怎么去掉小数点
/**
* 将日期转换为字符串 format:yyyyMMddHHmmss,d日 HH:mm:ss,HH:mm:ss,yyyy-MM-dd HH:mm:ss M月d日 HH:mm,yy年M月d日HH:mm,yy年M月d日HH:mm,yyyyMMddHHmmss.SSS
*/
public static String date2str(Date date, String format)
{
if (date == null)
{
return null;
}
SimpleDateFormat dateformat = new SimpleDateFormat(format);
dateformat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
return dateformat.format(date);
}
❺ java小数中如何去掉小数点后
来个最简单的
示例:
publicstaticvoidmain(String[]args){
doubled=10.111111d;
inti=(int)d;
System.out.println(i);
}
❻ jave 中bigdecimal 如何去掉小数位
用以下三种方法
toBigInteger() 如果数字很大用这个,返回一个BigInteger类的对象(JAVA中有这样的类,如果你的程序确实只要整数的话就用这个类吧。),小数部分直接CUT。
intValue() 返回int型
longValue() 返回long型
❼ java里用Math.round怎么吧小数点后面的0去掉
可参考如下代码:
Strings="111.01100";
if(s.indexOf(".")>0){
//正则表达
s=s.replaceAll("0+?$","");//去掉后面无用的零
s=s.replaceAll("[.]$","");//如小数点后面全是零则去掉小数点
}
System.out.println(Math.round(s));
JAVA API解释如下:
public static long round(double a)
returns the closest long to the argument. the result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. in other words, the result is equal to the value of the expression:
返回最接近参数的long。结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为long类型。换句话说,结果等于以下表达式的值:
(long)Math.floor(a + 0.5d)
特殊情况如下:
如果参数为 NaN,那么结果为 0。
如果结果为负无穷大或任何小于等于Long.MIN_VALUE的值,那么结果等于Long.MIN_VALUE的值。
如果参数为正无穷大或任何大于等于Long.MAX_VALUE的值,那么结果等于Long.MAX_VALUE的值。
参数:
a- 舍入为long的浮点值。
返回:
舍入为最接近的long值的参数值。
❽ Java有一个小数,如何去掉小数部分
1、Math.round(float f)对小数部分四舍五入
或者强值类型转换成int类型,直接去掉小数部分!
2、
3、向上取整:Math.ceil() //只要有小数都+1
向下取整:Math.floor() //不取小数
四舍五入:Math.round() //四舍五入
❾ java里如何在输出浮点数小数部分为0时自动将小数点和小数部分隐藏
可以直接以浮点数和其强转为整数的数值进行比较,相同则直接取整数,即去掉小数点;
否则,即不同时,原样输出;
PS.