0

The following code:

String a = "100.00";
String b = "10.00";
String c=  "5.00";

String value = a+ "\n"+ b +"\n" +c;
System.out.println(value);

Prints:

100.00
10.00
5.00

I need output in a format where decimal point position will be fixed without using any string format library (with logic):

100.00
 10.00
  5.00 

Variable values are coming from the database and requirement is to show values with the decimal point in the same position with values vertically.

5
  • 11
    Why without String format (assuming you mean the format method)? That would be the obvious path and you'd need a pretty strong argument on why you want to avoid using that. Commented Aug 29, 2018 at 14:31
  • Actual This issue I am facing in ABAP and there I cant use the format. So I am looking for the logic. Commented Aug 29, 2018 at 14:33
  • 1
    Find the longest string you gonna print and then prepend the missing amount of spaces to every other string. a loop will be needed Commented Aug 29, 2018 at 14:35
  • the logic is 1) find the longest string 2) for each string calculate the difference between the length of this string and the longest one 3) construct a string of space with that difference and concatenat it to your string Commented Aug 29, 2018 at 14:35
  • 1
    Try this: String value = a+ "\n "+ b +"\n " +c; :P Commented Aug 29, 2018 at 14:38

2 Answers 2

3

Here's an example using streams; probably could be more efficient but I was having fun.

It assumes all have 2 decimals as you showed, just FYI; that could be modified though.

String a = "100.00";
String b = "10.00";
String c=  "5.00";

List<String> strings = Arrays.asList(a, b, c);
final int maxLen = strings.stream().map(String::length).reduce(Math::max).get();

strings.forEach(s -> {
    System.out.println(Stream.generate(() -> " ").limit(maxLen - s.length()).collect(Collectors.joining()) + s);
});

Results:

100.00
 10.00
  5.00

I also put pthe 3 examples into a list so it would work on an arbitrary number of elements.

Without Java 8

String a = "100.00";
String b = "10.00";
String c =  "5.00";

List<String> strings = Arrays.asList(a, b, c);

int maxLen = -1;
for (String s : strings) maxLen = Math.max(maxLen, s.length());
for (String s : strings) {
    String spaces = "";
    for (int i = 0; i < maxLen - s.length(); ++i) {
        spaces += " ";
    }
    System.out.println(spaces += s);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is what needed. Thanks for this. But can we have this code without java8?
Sure, added an alternate solution.
1

I don't know what 'don't use any String format library' means specifically, but... now we get into a semantic argument as to what 'library' means. You're formatting a string. If I take your question on face value you're asking for literally the impossible: How does one format a string without formatting a string?

I'm going to assume you meant: Without adding a third party dependency.

In which case, if you have doubles (or BigDecimal or float) as input:

String.format("%6.2f", 100.00);
String.format("%6.2f", 10.00);
String.format("%6.2f", 5.00);

(6? Yes; 6! The 6 is the total # of characters to print at minimum. You want 3 digits before the separator, a separator, and 2 digits after, that's a grand total of 6, hence the 6.)

If you have strings as input, evidently you are asking to right-justify them? Well, you can do that:

String.format("%6s", "100.00");
String.format("%6s", "10.00");
String.format("%6s", "5.00");

NB: For convenience, System.out.printf("%6s\n", "100.0"); is short for System.out.println(String.format("%6s", "100.0"));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.