sameer joshi
sameer joshi

Reputation: 377

How to parse String having decimal, negative decimal into integer?

Under for loop i am getting string values like decimal,integer, negative decimal values, i want to convert all of them in integer with one if condition,

how to achieve this?

 for(WebElement rowElement:TotalRowCount)
        {
              List<WebElement> TotalColumnCount=rowElement.findElements(By.xpath("td/span"));
              int ColumnIndex=1;
              for(WebElement colElement:TotalColumnCount)
              {
                  String s=colElement.getText().replace("%", "");

                  if(!s.isEmpty()&&!s.equals("NA"))
                  {

                   System.out.println("Row "+RowIndex+" Column "+ColumnIndex+" Data "+s);

                  }
                  else{/*do nothing*/}
               }
              RowIndex=RowIndex+1;
         }

Upvotes: 0

Views: 2211

Answers (6)

Procrastinator
Procrastinator

Reputation: 2664

You can check first that if a received String is numeric or not by the following method and put that method call as a condition in your if statement.

public static boolean isNumeric(String s) {
        if (s.isEmpty())
            return false;
        try {
            double d = Double.parseDouble(s);
        }
        catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }

So, your If condition in for loop should look something like if(isNumeric(s)){...}. Also, to parse these values only to the Integer you can cast them by Math.floor(Double.parseDouble(s) + 0.5d). I think that should solve the issue.

Upvotes: 1

Shubhendu Pramanik
Shubhendu Pramanik

Reputation: 2751

As your input may be decimal,integer, negative try below code to get proper rounded off result:

String s = "-88.4";
int a = Math.round(Float.parseFloat(s));
System.out.println(a);

Upvotes: 0

Dev
Dev

Reputation: 6776

 for(WebElement rowElement:TotalRowCount)
        {
              List<WebElement> TotalColumnCount=rowElement.findElements(By.xpath("td/span"));
              int ColumnIndex=1;
              for(WebElement colElement:TotalColumnCount)
              {
                  String s=colElement.getText().replace("%", "");

                  if(!s.isEmpty()&&!s.equals("NA"))
                  {

                   System.out.println("Row "+RowIndex+" Column "+ColumnIndex+" Data "+s);
         int val = (int) Double.parseDouble(s); // ==> This is val in integer

                  }
                  else{/*do nothing*/}
               }
              RowIndex=RowIndex+1;
         }

Upvotes: 1

RP-
RP-

Reputation: 5837

If you are expecting decimals, then you can use BigDecimal.

BigDecimal nm = new BigDecimal(s);
int required = nm.setScale(0, RoundingMode.DOWN).intValueExact();

Upvotes: 1

FreedomPride
FreedomPride

Reputation: 1102

Declare it above.

int result = 0;

As per your code ,

String s=colElement.getText().replace("%", "");
result = Integer.parseInt(s);

Now result holds the integer value that you need to check.

Upvotes: 0

Zakaria Shahed
Zakaria Shahed

Reputation: 2697

int intnum= Integer.parseInt("1234");

See the Java Documentation for more information.

(If you have it in a StringBuilder (or the ancient StringBuffer), you'll need to do Integer.parseInt(myBuilderOrBuffer.toString()); instead).

Upvotes: 1

Related Questions