-1

in Java 7, I need to perform multiple formatting operations on a double before further processing it. I currently do it like so:

double price = lemon.getPrice(); // Something like 20.2 or 19.235
String priceWith2Digits = String.format("%.02f", price); // 20.20
String priceWithDecimalComma = priceWith2Digits.replace('.', ','); // 20,20
String pricePadded = selfMadePaddingFunction(priceWithDecimalComma, 10, ' '); // "     20,20"
result.setPrice(pricePadded);

Now I wonder if there is a way to increase readability while reducing the number of String variables.
Edit: This question is not about padding, its about increasing code readability, which is not done by doing everything in one row.

Hereks
  • 158
  • 1
  • 8
  • 1
    And well, when you read how to do format patterns, **padding** is explained as well. Most likely, you can do all of the formatting with a single call. So, simply study the rules for formatting patterns in more depth. – GhostCat Apr 08 '22 at 10:13
  • Obviously I can concat everything into one call, but that becomes harder to read than my current implementation. – Hereks Apr 08 '22 at 10:53
  • 1
    No, that is *not* obvious. If you use the standard library, and use a readable formatting pattern (based again on the java standards) ... then a single call with a single pattern is more readable. Because any decent java programmer should be able to understand what is going on. Just see the answer you got from Mark. Does that look "more" complicated than what you have above? – GhostCat Apr 08 '22 at 10:59
  • Yes every decent java programmer will understand the one liner, just like every decent java programmer will understand Marks answer and my original code. But I want the effort to understand whats actually going on to be minimal. And I think a oneliner requires more effort than my original code. – Hereks Apr 08 '22 at 11:06
  • Effort in terms of runtime performance? Then Marks solution probably beats yours. The expensive part is establish the setup to process the pattern, and your solution already does that. But your solution also includes the cost to iterate the string for the replace. Seriously: if the performance penalty for Mark makes a real significant difference for your workload ... then you would be in a very luxurious position. And note: in the end, java performance comes out of the JIT. And the JIT can optimize SMALL straight forward code MUCH better. – GhostCat Apr 08 '22 at 11:23
  • Efforts in terms of programmers dealing with the code? Your solution is about reading multiple lines of code, it is about understanding what your custom padding function does, it is about PUSHING aside the thoughts of "why isn't he just using format() to do all of that at once"? It very much depends on your audience what "efficiency" means here. And when it is only about yourself: rather adapt to the short solution by Mark. Because that is the canonical way of solving such things. THAT is the sort of code you should be writing, and you should "like" to read. – GhostCat Apr 08 '22 at 11:25
  • When you are in Rome, do as the Romans do. So, long story short, "efficiency" points to Marks answer, too. I can understand that it can give a new java programmer some trouble, but as said: that is the preferred way to do it in Java. You can decide to deviate from that, but deviation comes at cost. – GhostCat Apr 08 '22 at 11:27
  • 1
    Not in terms of runtime performance. Sorry, I thought i put enough emphasis on the readability aspect. I get that readability differs by audience but I thought there is some general consensus or way to perfom multiple formatting operations which is not doing everything in one line. In this case, the audience is other employees that work on the same code basis. Your comment leads me to thinking that the most readable way therefore, is Marks answer. – Hereks Apr 08 '22 at 11:37
  • You are right, and that is an aspect I discussed with other folks many times: "readability" depends on skill levels. For a total newbie, your initial solution might be just fine. Because your code does things (mostly) in a very explicit way. And that is what people do initially: write a lot of "low level" code that does things "manually". But then, the more you understand about the standard libraries, you better change. You prefer to do things on a "higher" level, *assuming* that every reader is familiar with the abstractions you/the library calls are making. – GhostCat Apr 08 '22 at 12:04

1 Answers1

2

You can do this in one statement by using a Locale that uses a , as the decimal separator (e.g. Locale.GERMANY), and specifying the width format option:

double price = 20.2;
String formatted = String.format(Locale.GERMANY, "%10.2f", price);
System.out.println(formatted);

Output:

     20,20

See the format string syntax for details.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197