简体   繁体   English

Java将double转换为int

[英]Java Convert double to int

I have this code, and the input in the textfield is 50, so its output should be 25, 我有此代码,文本字段中的输入为50,因此其输出应为25,
but there is a error, because no output 但是有错误,因为没有输出

button_1.addActionListener(new ActionListener() {
    String tfg = textField.getText().toString();
    String tfc = textField.getText();

    public void actionPerformed(ActionEvent e) {
        if(textField.getText().toString().equals("0") || 
            textField.getText().toString().equals("") || 
            textField.getText().toString().equals(" ") || 
            textField.getText().toString().matches("[a-zA-Z]+")) {
            JOptionPane.showMessageDialog(Cow.this,
                            "Tree Amount Should Be 1 or More",
                            "Invalid Tree Amount",
                            JOptionPane.ERROR_MESSAGE);
        }

        if(!tfg.equals("0")) {
            try {
                int tfgg = Integer.parseInt(tfc);
                int trcs = tfgg * 1;
                double trcp = 1 / 2;
                int trcc = (int) trcp * trcs;
                System.out.println(new Integer(trcc).toString());
            } catch (NumberFormatException se) {

            }
        }
    }
});

I try to convert from my php to that code, here is my php code : 我尝试将我的php转换为该代码,这是我的php代码:

$doorSeed = $mount * 1;
$doorPrice = 1 / 2;
$doorConvert = $doorPrice * $doorSeed;
echo "Treasure Chest Seed = ".$doorSeed." Price : ".$doorConvert." World Lock <br>";
int trcc = (int) trcp * trcs;

Try this instead 试试这个

int trcc = (int) (trcp * trcs);

You need to cast the complete expression [ (trcp * trcs) ] instead of just the first variable trcp to int . 您需要将complete expression [ (trcp * trcs) ] (trcp * trcs)而不只是将第一个变量trcpint The other variable in the expression trcs is of double type, so the result of the expression become double . 表达式trcs的另一个变量为double类型,因此表达式的结果变为double That is why you cast the complete expression. 这就是为什么要强制转换完整的表达式。 So your end result will be int 所以你的最终结果将是int

     int tfgg = Integer.parseInt(tfc.trim());    // tfc = 4;   tfgg = 4;
     int trcs = tfgg * 1;                 // trcs = 4 * 1 = 5;
     double trcp = 1.0 / 2;               // trcp = 0.5;
     int trcc = (int) (trcp * trcs);      // trcc = (0.5 * 5) = (2.5) = 2;
     System.out.println(trcc);            // 2;

double trcp = 1.0 / 2;

Here make at least one value as double so the expression will be evaluated as double as suggested by @tunaki. 在这里,至少将一个值设为double以便表达式将按@tunaki的建议计算为double

Also you don't need this statement in your code int trcs = tfgg * 1; 同样,您的代码中也不需要此语句int trcs = tfgg * 1; .

Use like this: 像这样使用:

  String tfg ;
  String tfc ;
  public void actionPerformed(ActionEvent e) {
    tfg = textField.getText();
    tfc = textField.getText();
    if(textField.getText().equals("0") || textField.getText().equals("") || textField.getText().equals(" ") || textField.getText().matches("[a-zA-Z]+")) {
      JOptionPane.showMessageDialog(Cow.this,
          "Tree Amount Should Be 1 or More",
          "Invalid Tree Amount",
          JOptionPane.ERROR_MESSAGE);
    }
    if(!tfg.equals("0")) {
      try {
        int tfgg = Integer.parseInt(tfc.trim());    // tfc = 4;
        int trcs = tfgg;                 // trcs = 4 * 1 = 5
        double trcp = 1.0 / 2;                 // trcp = 0.5
        int trcc = (int) (trcp * trcs);      // trcc = (0.5 * 5) = (2.5) = 2
        System.out.println("HI: "+trcc);
      } catch (NumberFormatException ignored) {
      }

Your problem is at this line: 您的问题在这一行:

double trcp = 1 / 2;

This does not result in trcp = 0.5 but trcp = 0.0 , because you are dividing integer values (and so are using integer division). 这不会导致trcp = 0.5不会导致trcp = 0.0 ,因为您正在对整数值进行除法(因此也将使用整数除法)。

You should use the following code: 您应该使用以下代码:

double trcp = 1.0 / 2;

to force division using doubles. 强制使用双打进行划分。

Other comments: 其他的建议:

  • new Integer(trcc).toString() should be replaced with String.valueOf(trcc) . new Integer(trcc).toString()应该替换为String.valueOf(trcc)
  • Avoid using empty catch statements: at minimum, log the exception 避免使用空的catch语句:至少记录异常
  • What's the point of this line: int trcs = tfgg * 1; 这行的意义是什么: int trcs = tfgg * 1; ?

Basically you have this (int)0.5 = 0 . 基本上,您有(int)0.5 = 0 In your case, trcp = 0.5 and is double, so when it's cast to integer the result is 0. 在您的情况下, trcp = 0.5且为double值,因此当将其trcp = 0.5为整数时,结果为0。

And when you do (int)a + b , the true operation with parenthesis for priority here is: ((int)a)+(b) , so what you have to is the following: 当您执行(int)a + b ,此处带括号的优先级运算为((int)a)+(b) ,因此您需要执行以下操作:

int trcc = (int) (trcp * trcs);

instead of the following: 而不是以下内容:

int trcc = (int) trcp * trcs;

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

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