为什么在这种情况下 Java 的子字符串没有起作用?

huangapple go评论61阅读模式
英文:

Why java substring not working in this case?

问题

我已经编写了一段代码,该代码可以将Java代码中小数点前的数字选取并存储在不同的变量中,将小数点后的数字选取并存储在不同的变量中。例如,在下面的代码中,我们有23.256。现在,我编写的代码将选择23存储在firstDigit变量中,.256存储在lastDigit变量中。问题出现在当小数点后没有数字时,例如,如果我们有23,代码将会崩溃。

我已经编写的代码(这个代码可行)

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());

它不起作用

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将由用户提供,因此我们不知道小数点后是否有数字。问题是,当用户输入没有小数点后数字的totalAmount时,代码会崩溃。

任何帮助将不胜感激 为什么在这种情况下 Java 的子字符串没有起作用?

英文:

I've written a code where java code selects digit before decimal point in different variable and digits after decimal point in different variable. 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. Now problem comes when there is no digits after decimal point for example if we have 23 this will crash.

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. Problem when user enters the totalAmount without any digit after decimal point the code crashes.

Any help will be appreciated 为什么在这种情况下 Java 的子字符串没有起作用?

答案1

得分: 1

你需要首先检查索引是否为“-1”,表示未找到小数点。

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

You need to first check if the index is -1, meaning that no decimal point was found.

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

答案2

得分: 1

以下代码对我起作用。

double totalAmount = 23.256;
String totalAmountInString = String.valueOf(totalAmount).toString();
//检查totalAmount是否在小数点后有值
if (totalAmount % 1 != 0)
{
     //totalAmount在小数点后有值
     String firstDigit = totalAmountInString.substring(0, totalAmountInString.indexOf('.'));
     String lastDigit = totalAmountInString.substring(totalAmountInString.indexOf('.'), totalAmountInString.length());
     System.out.println("小数点前: " + firstDigit);
     System.out.println("小数点后: " + lastDigit);
}
else
{
     //totalAmount在小数点后没有值
     System.out.println(totalAmountInString + " 没有小数部分");
}

祝好运
英文:

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!

答案3

得分: 0

你可以使用String类的split方法。

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];
}
英文:

You can use split method of String 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];
	}

huangapple
  • 本文由 发表于 2020年7月24日 12:04:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63066628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定