简体   繁体   English

为什么 java substring 在这种情况下不起作用?

[英]Why java substring not working in this case?

I've written a code where java code selects digit before decimal point in different variable and digits after decimal point in different variable.我编写了一个代码,其中 java 代码选择不同变量中小数点前的数字和不同变量中小数点后的数字。 For example in the below code we have 23.256 Now the code that i have written will select 23 in firstDigit variable and.256 in lastDigit variable.例如在下面的代码中,我们有 23.256 现在我编写的代码将 select 23 在firstDigit变量和 256 在lastDigit变量。 Now problem comes when there is no digits after decimal point for example if we have 23 this will crash.现在问题来了,当小数点后没有数字时,例如如果我们有 23,这将崩溃。

What I've written is (THIS WORKS)我写的是(这工作)

totalAmount = 23.256;
String totalAmountString = new BigDecimal(String.valueOf(totalAmount )).toString();
String firstDigit= totalAmountString .substring( 0,totalAmountString .indexOf('.'));
String lastDigit= totalAmountString .substring( totalAmountString .indexOf('.'), totalAmountString .length());

It doesn't work它不起作用

totalAmount = 23;
String totalAmountString = new BigDecimal(String.valueOf(totalAmount )).toString();
String firstDigit= totalAmountString .substring( 0,totalAmountString .indexOf('.'));
String lastDigit= totalAmountString .substring( totalAmountString .indexOf('.'), totalAmountString .length());

totalAmount will be provided by user so we don't know if there will digit after decimal point or not. totalAmount 将由用户提供,所以我们不知道小数点后是否有数字。 Problem when user enters the totalAmount without any digit after decimal point the code crashes.当用户在小数点后没有任何数字输入 totalAmount 时出现问题,代码崩溃。

Any help will be appreciated:)任何帮助将不胜感激:)

You need to first check if the index is -1 , meaning that no decimal point was found.您需要首先检查索引是否为-1 ,这意味着没有找到小数点。

int idx = totalAmountString .indexOf('.');
String firstDigit=  idx != -1 ? totalAmountString .substring( 0, idx): totalAmountString;
String lastDigit= idx != -1 ? totalAmountString .substring(idx, getAcreIntoString.length()): "";

The following code worked for me.以下代码对我有用。

double totalAmount = 23.256;
String totalAmountInString = String.valueOf(totalAmount).toString();
//Checks wether the totalAmount has value after decimal     
if (totalAmount % 1 != 0)
{
     //totalAmount has value after decimal
     String firstDigit = totalAmountInString.substring( 0,totalAmountInString .indexOf('.'));
     String lastDigit= totalAmountInString.substring( totalAmountInString.indexOf('.'), totalAmountInString.length());
     System.out.println("Before Decimal : " + firstDigit);
     System.out.println("After Decimal : " + lastDigit);
}
else
{
     //totalAmount has no value after decimal
     System.out.println(totalAmountInString + " Has no decimal");
}

Good Luck!祝你好运!

You can use split method of String class您可以使用字符串 class 的拆分方法

   String totAmountStr = new BigDecimal(String.valueOf(23.256)).toString();
    String[] split = totAmountStr.split("\\.");
    String firstDigit= null;
    String lastDigit= null;;
    if (split.length > 1) {
         firstDigit = split[0];
         lastDigit = split[1];
    } else {
         firstDigit = split[0];
    }

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

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