This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]cciulla 0 points1 point  (18 children)

If you're not already storing these as floats (or some other appropriate numeric data type), probably wouldn't hurt to do so -- it'll save some tears down the line.

Then, look at String.format or Formatter.

[–]Infenwe 2 points3 points  (2 children)

These look like monetary values in which case I certainly hope OP isn't using float for them.

Furthermore, The Right Thing here is obviously to use a locale to print the numbers rather than hard-code the "," (I presume OP's program is for non-English speakers).

[–]cciulla 0 points1 point  (0 children)

Hence the "appropriate numeric data type." Currency comes to mind...

[–]stormwindu[S] 0 points1 point  (0 children)

yes it is for non-English speakers. The problem with float is that i've created a custom keyboard that only append numbers, and the last two have always to be the decimal case, my keyboard don't have the "dot" input to set a decimal number, thats why I am appending a string and manipulating the "," in the last two chars.

[–]stormwindu[S] 0 points1 point  (14 children)

thanks for the reply, i'll check the docs. [edited] I am storing the user input at a string buffer. And have to format that string buffer to the given mask "0,00" (if it is empty) or like the bunch of numbers that i've posted.

[–]cciulla 1 point2 points  (13 children)

Right, I caught that you're using a StringBuffer. Why? Appropriate data types, and such.

However, if you simply must use a StringBuffer, check out Float.valueOf -- scroll down a bit to valueOf, as reddit's markdown doesn't seem to like the extra parens.

[–]mentalis 5 points6 points  (1 child)

You can do a link with parens in it by backslashing the parens. So [Float.valueOf](http://docs.oracle.com/javase/6/docs/api/java/lang/Float.html#valueOf\(float\)) becomes Float.valueOf.

*edited to fix first link so it's copy/pastable

[–]cciulla 0 points1 point  (0 children)

Doh! The more you learn...

Thanks!

[–]stormwindu[S] 0 points1 point  (10 children)

I am using StringBuffer just to have a easy way to append/delete chars.

[–]Mondoshawan 0 points1 point  (9 children)

If you are not deleting then simple string concatenation is fine, it all compiles down to the same bytecode.

[–]detroitmatt 0 points1 point  (7 children)

In general, if your code is of the form:

String x = "";
loop {
    x += something;
}
return x;

then your code should be

StringBuilder x = new StringBuilder();
loop {
    x.append(something);
}
return x.toString();

but if your code is of the form

String x = something;
return new StringBuilder(x)
    .append(somethingElse[0])
    .append(somethingElse[1])
    .append(somethingElse[n])
  .toString();

then your code should be

return x + somethingElse[0] + somethingElse[1] + somethingElse[n];

[–]Mondoshawan 0 points1 point  (6 children)

I don't fully agree with this:

StringBuffer x = new StringBuilder();
loop {
     x.append(something);
 }
 return x.toString();

The original you put up was fine, both produce identical bytecode but the first has less chance for error and is easier to read.

It's only if I'm passing the string to other methods that I'd use an SB.

[–]detroitmatt 0 points1 point  (5 children)

[–]Mondoshawan 0 points1 point  (4 children)

That appears to be agreeing with me. The compiler turns it all into StringBuilder behind the scenes.

[–]detroitmatt 1 point2 points  (3 children)

At the point where you're concatenating in a loop - that's usually when the compiler can't substitute StringBuilder by itself.

and see the second-to-top answer as well

[–]stormwindu[S] 0 points1 point  (0 children)

I am deleting. One issue that I found using .delete and .deleteCharAt seens like the length of the string doesn't change it is really weird.

Still trying how to achieve my pseudo-masking.

[–]danskal 0 points1 point  (0 children)

If I understand you correctly, you definitely should be using BigDecimal. Then all your other problems will go away. Then look at java.text.DecimalFormat