patternjavaModerate
Converting a numerical string to the equivalent multipled by 100
Viewed 0 times
theequivalentnumericalmultipled100convertingstring
Problem
Method:
Requirements:
I cannot use the
For example:
Here is my code, and I would like to know if there are any cavities in it:
- Input: a string which represents an integer or a non-integer value.
- Output: a string which represents the same value multiplied by 100.
Requirements:
- No floating-point unless needed.
- No leading 0s in the part left of the floating-point.
- No trailing 0s in the part right of the floating-point.
I cannot use the
Double.parseDouble method, because it sometimes yields an "ugly" result.For example:
String.valueOf(Double.parseDouble("0.0071")*100)); // yields 0.7100000000000001
String.valueOf(Double.parseDouble("0.072" )*100)); // yields 7.199999999999999Here is my code, and I would like to know if there are any cavities in it:
String MultiplyBy100(String number)
{
StringBuilder sb = new StringBuilder();
String[] parts = number.split("\\.");
if (parts.length == 1)
sb.append(parts[0]+"00");
else if (parts[1].length() == 1)
sb.append(parts[0]+parts[1]+"0");
else if (parts[1].length() == 2)
sb.append(parts[0]+parts[1]);
else
sb.append(parts[0]+parts[1].substring(0,2)+"."+parts[1].substring(2));
if (sb.charAt(0) == '-')
{
while (sb.length() > 2 && sb.charAt(1) == '0' && sb.charAt(2) != '.')
sb.deleteCharAt(1);
}
else
{
while (sb.length() > 1 && sb.charAt(0) == '0' && sb.charAt(1) != '.')
sb.deleteCharAt(0);
}
return sb.toString();
}Solution
-
It would be much easier with
It works with big numbers well:
Two readings:
-
The original code returns
-
Java developers usually start method names with lowercase letters.
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
Source: Code Conventions for the Java TM Programming Language, 9 - Naming Conventions
It would be much easier with
BigDecimals:String input = "00007.880000";
BigDecimal hundred = BigDecimal.valueOf(100);
BigDecimal result = new BigDecimal(input).multiply(hundred).stripTrailingZeros();
System.out.println(result.toString()); // prints "788"It works with big numbers well:
String input = "0000123456789123456789.123456789123456789123456789123456789880000";
BigDecimal hundred = BigDecimal.valueOf(100);
BigDecimal result = new BigDecimal(input).multiply(hundred).stripTrailingZeros();
// prints "12345678912345678912.345678912345678912345678912345678988"
System.out.println(result.toString());Two readings:
- Effective Java, 2nd Edition, Item 48: Avoid float and double if exact answers are required
- Why not use Double or Float to represent currency?
-
The original code returns
788.0000 for 00007.880000 which I guess does not fulfill the specification.-
Java developers usually start method names with lowercase letters.
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
Source: Code Conventions for the Java TM Programming Language, 9 - Naming Conventions
Code Snippets
String input = "00007.880000";
BigDecimal hundred = BigDecimal.valueOf(100);
BigDecimal result = new BigDecimal(input).multiply(hundred).stripTrailingZeros();
System.out.println(result.toString()); // prints "788"String input = "0000123456789123456789.123456789123456789123456789123456789880000";
BigDecimal hundred = BigDecimal.valueOf(100);
BigDecimal result = new BigDecimal(input).multiply(hundred).stripTrailingZeros();
// prints "12345678912345678912.345678912345678912345678912345678988"
System.out.println(result.toString());Context
StackExchange Code Review Q#44848, answer score: 11
Revisions (0)
No revisions yet.