當前位置:首頁 » 編程語言 » java去掉小數點

java去掉小數點

發布時間: 2022-05-14 07:03:14

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.

熱點內容
android敏捷開發 發布:2025-05-11 11:56:49 瀏覽:79
腳本pon 發布:2025-05-11 11:52:27 瀏覽:825
ct5推薦哪個配置 發布:2025-05-11 11:47:45 瀏覽:741
領購未上傳發票 發布:2025-05-11 11:43:27 瀏覽:716
查看華為雲伺服器的ip地址 發布:2025-05-11 11:24:44 瀏覽:235
長沙銀行密碼多少 發布:2025-05-11 11:24:38 瀏覽:671
緩存手機視頻合並軟體哪個好 發布:2025-05-11 11:22:30 瀏覽:698
伺服器c盤怎麼清除 發布:2025-05-11 11:16:33 瀏覽:39
動態估演算法 發布:2025-05-11 11:06:19 瀏覽:923
sql2008使用教程 發布:2025-05-11 10:53:16 瀏覽:315