如何将负整数转换为正整数

时间:2021-06-03 16:01:17
String v1 = lbl_READING_NUMBER.getText();


int a = Integer.parseInt(jLabel_PREVIOUS_READ.getText());

int b = Integer.parseInt(jLabel_PRESENT_READ.getText());

int cm = a-b;

try{

    String sql = "UPDATE reading SET Cubic_meter=' "+cm+" ' WHERE Reading_Number=' "+v1+"' ";

    ps = conn.prepareStatement(sql);

    rs=ps.executeUpdate();
}
catch(Exception e){

    JOptionPane.showMessageDialog(null, e);

}

This code display negative Cubic_meter column in my database, want to have a positive Cubic_meter even if the jLabel_PREVIOUS_READ.getText() is lesser than jLabel_PRESENT_READ.getText().

此代码在我的数据库中显示负Cubic_meter列,即使jLabel_PREVIOUS_READ.getText()小于jLabel_PRESENT_READ.getText(),也希望具有正Cubic_meter。

4 个解决方案

#1


4  

It sounds like you might be looking for Math.abs:

听起来你可能正在寻找Math.abs:

Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

返回int值的绝对值。如果参数不是负数,则返回参数。如果参数为负数,则返回参数的否定。

(There are also versions for other types — long, float, double...)

(还有其他类型的版本 - 长,浮动,双...)

There's an important caveat:

有一个重要的警告:

Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

请注意,如果参数等于Integer.MIN_VALUE的值,即最负的可表示的int值,则结果是相同的值,即负值。

#2


1  

Do Math.abs like T.J. Crowder suggested, but also do

做Math.abs就像T.J.克劳德建议,但也做

catch NumberFormatException when parsing the input

解析输入时捕获NumberFormatException

Change your SQL statement to use bound variables, like this

更改您的SQL语句以使用绑定变量,如下所示

    String sql = "UPDATE reading SET Cubic_meter=? WHERE Reading_Number= ?";
    ps = conn.prepareStatement(sql);
    ps.setInt(1, cm); // assuming always positive
    ps.setInt(2, v1);

http://en.wikipedia.org/wiki/Prepared_statement

http://en.wikipedia.org/wiki/Prepared_statement

Using bound variable is a good practice to guard against SQL injection.

使用绑定变量是防止SQL注入的好习惯。

#3


0  

Can you use this?

你能用这个吗?

 value=value*-1; //simply multiply by -1. 

You can also set a condition to check if the value is negative with an if statement such as.

您还可以使用if语句设置条件以检查值是否为负数。

 if(value<0)
   value=value*-1; //simply multiply by -1. 

#4


0  

Try this:

尝试这个:

int cm = a-b;
int cm2 = cm*(-1);

try{

    String sql = "UPDATE reading SET Cubic_meter=' "+cm2+" ' WHERE Reading_Number=' "+v1+"' ";

    ps = conn.prepareStatement(sql);

    rs=ps.executeUpdate();
}
catch(Exception e){

    JOptionPane.showMessageDialog(null, e);

}

#1


4  

It sounds like you might be looking for Math.abs:

听起来你可能正在寻找Math.abs:

Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

返回int值的绝对值。如果参数不是负数,则返回参数。如果参数为负数,则返回参数的否定。

(There are also versions for other types — long, float, double...)

(还有其他类型的版本 - 长,浮动,双...)

There's an important caveat:

有一个重要的警告:

Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

请注意,如果参数等于Integer.MIN_VALUE的值,即最负的可表示的int值,则结果是相同的值,即负值。

#2


1  

Do Math.abs like T.J. Crowder suggested, but also do

做Math.abs就像T.J.克劳德建议,但也做

catch NumberFormatException when parsing the input

解析输入时捕获NumberFormatException

Change your SQL statement to use bound variables, like this

更改您的SQL语句以使用绑定变量,如下所示

    String sql = "UPDATE reading SET Cubic_meter=? WHERE Reading_Number= ?";
    ps = conn.prepareStatement(sql);
    ps.setInt(1, cm); // assuming always positive
    ps.setInt(2, v1);

http://en.wikipedia.org/wiki/Prepared_statement

http://en.wikipedia.org/wiki/Prepared_statement

Using bound variable is a good practice to guard against SQL injection.

使用绑定变量是防止SQL注入的好习惯。

#3


0  

Can you use this?

你能用这个吗?

 value=value*-1; //simply multiply by -1. 

You can also set a condition to check if the value is negative with an if statement such as.

您还可以使用if语句设置条件以检查值是否为负数。

 if(value<0)
   value=value*-1; //simply multiply by -1. 

#4


0  

Try this:

尝试这个:

int cm = a-b;
int cm2 = cm*(-1);

try{

    String sql = "UPDATE reading SET Cubic_meter=' "+cm2+" ' WHERE Reading_Number=' "+v1+"' ";

    ps = conn.prepareStatement(sql);

    rs=ps.executeUpdate();
}
catch(Exception e){

    JOptionPane.showMessageDialog(null, e);

}