導航:首頁 > 編程語言 > javadouble大小比較

javadouble大小比較

發布時間:2022-08-14 17:12:10

『壹』 java中兩個字元串如何比較大小

有三種方法實現

第一種直接用字元串類的compareTo方法:

Stringt1="20131011";
Stringt2="20131030";
intresult=t1.compareTo(t2);

第二種是把這個日期字元串轉換成long:

SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");
Dated1=sdf.parse(t1);
Dated2=sdf.parse(t2);
longresult=d1.getTime()-d2.getTime();

第三種是把日期字元串轉換成整形int:

intint1=Integer.parseInt(t1);
intint2=Integer.parseInt(t2);
intresult=int1-int2;

註:result大於0,則t1>t2;

result等於0,則t1=t2;

result小於0,則t1<t2;

『貳』 java中double與Integer直接比較大小是否可以

當然可以直接比較了,比較中會把integer的轉型為double再比較大小的

『叄』 Java中Double的比較

Double沒有的,double有 Double是對象,看兩個對象是否值相同,調用 equals方法
這也是解決double精度比較的方法。另外還有一種方法,你可以用寫一個 double成員屬性,然後在eclipse中重寫equals方法,你會看到他是怎麼處理兩個double值是否相同的 sun公司用的是Double.doubleToLongBits(要比較的double值) != Double.doubleToLongBits(另一個double值) 這種方式

『肆』 JAVA比較2個數大小

你的代碼我改了一下
這個應該是符合你的要求的
import java.awt.*;
import java.awt.event.*;

public class ActionEventDemol implements WindowListener{
Frame f = new Frame("ActionEventDemol");
Label msg = new Label("第一個數:",Label.CENTER);
Label msg1 = new Label("第二個數:",Label.CENTER);
Label msg2 = new Label("大的數:",Label.CENTER);
Label msg3 = new Label("",Label.CENTER);

TextField name = new TextField(10);

TextField output = new TextField(10);
Button bConfirm = new Button("確定");
Button bReset = new Button("重置");
Listener lsn = new Listener(this);
public ActionEventDemol(){
f.setLayout(null);
f.add(msg);
f.add(msg2);
f.add(name);
f.add(output);
f.add(bConfirm);
f.add(bReset);
f.add(msg1);
f.add(msg3);
msg.setBounds(20,40,120,10);
msg1.setBounds(20,60,120,10);
msg2.setBounds(20,90,50,10);
msg3.setBounds(30,90,120,10);
name.setBounds(140, 40, 140, 20);
output.setBounds(140, 60,140, 20);
bConfirm.setBounds(40, 110, 70, 20);
bReset.setBounds(190, 110, 70, 20);
f.setSize(300,150);
f.setVisible(true);
bConfirm.addActionListener(lsn);
bReset.addActionListener(lsn);
f.addWindowListener(this);

}
public static void main(String[] args){
new ActionEventDemol();
}

public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e){
System.exit(0);
}
class Listener implements ActionListener{

ActionEventDemol ob;
Listener(ActionEventDemol ob){
this.ob = ob;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == ob.bConfirm){
String s1 = name.getText();
String s2 = output.getText();

double d1;
double d2;
try{
d1 =Double.parseDouble(s1);
d2 =Double.parseDouble(s2);

if(d1>d2)
msg3.setText(s1);
else
msg3.setText(s2);
name.setText("");
output.setText("");
}catch (NumberFormatException e1){
msg3.setText("請輸入數字");
}
}else if(e.getSource()==ob.bReset){
ob.name.setText("");
}
ob.output.setText("");
}
}
}

『伍』 java中double類型怎麼判斷大於0

double i =8;
if(i>0){
System.out.println("大於0");
}

『陸』 java double和float的哪個大

float:4個位元組,32位
double:8個位元組,64位

『柒』 java小數比較

double類型的數值比較應該使用差值的絕對值
例如:public class DoubleCompare
{
public static void main(String[] args)
{
float a = 0.6f;
double b = 0.6;

System.out.println(a == b);
System.out.println(Math.abs(a - b) <= 0.0000001);
}
}

第一個列印為false,第二個為true

閱讀全文

與javadouble大小比較相關的資料

熱點內容
分段加密的安全性 瀏覽:507
咪咕直播為什麼沒有適配安卓系統 瀏覽:172
php模版大全 瀏覽:102
沒車能解壓嗎 瀏覽:634
php開發oa系統源碼 瀏覽:759
怎麼安裝蘋果ios的app 瀏覽:581
app拉新如何機刷 瀏覽:480
zendeclipseforphp 瀏覽:480
同時有幾個微信如何加密微信 瀏覽:86
大眾20t壓縮比 瀏覽:566
程序員要記住的500個單詞 瀏覽:830
wq快捷方式在哪個文件夾 瀏覽:965
雲南到河北源碼 瀏覽:92
安卓手機怎麼玩造夢3 瀏覽:60
多玩我的世界盒子怎麼創造伺服器地址 瀏覽:986
手機如何下載米家app 瀏覽:96
未知來源app在哪裡 瀏覽:206
命令與征服3合集 瀏覽:752
有免稅標簽有溯源碼是假的嗎 瀏覽:218
stc89c52單片機引腳圖 瀏覽:223