1

I'm creating an app from java to print sales check:

Locale locale = new Locale("EN", "US");
NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);

final private String contentTicket = "\n"
            + "_________________________________________\n"
            + "TOTAL:                      " + formatter.format(total) + "\n"
            + "PAY WITH:                   " + formatter.format(payWith) + "\n"
            + "CHANGE:                     " + formatter.format(change) + "\n"
            + "_________________________________________\n"
            + "      Thank you, have a good day...!!\n"
            + "=========================================\n";

The problem that I have is the alignment when I use the locale formatter for the monetary values. For example I want the output result be like this:

    _________________________________________
    TOTAL:                       $5,000.00
    PAY WITH:                   $10,000.00
    CHANGE:                      $5,000.00
    _________________________________________
          Thank you, have a good day...!!
    =========================================

But, I'm getting this:

    _________________________________________
    TOTAL:                      $5,000.00
    PAY WITH:                   $10,000.00
    CHANGE:                     $5,000.00
    _________________________________________
          Thank you, have a good day...!!
    =========================================

The variables total, payWith and change are all Double values.

Thanks in advance...

CyborgNinja23
  • 290
  • 12
  • 33
  • 1
    Instead of using white SPACE like " ", you can use "\t\t" which will make TABs instead of SPACEs, this will help you alllign your text. – chenchuk Nov 28 '15 at 13:50
  • I suppose your problem is with spacing, and has nothing to do with locale. Firstly are you trying to use java to print the output, you should use some presentation layer, like report building tools, or html. Anyways if you want to use java, then adjust your number of white spaces – Saurabh Jhunjhunwala Nov 28 '15 at 14:01
  • @Saurabh Jhunjhunwala I'm showing the output in java for testing, for printing purposes, thats why I'm making this question... – CyborgNinja23 Nov 28 '15 at 14:35

1 Answers1

1

This seems alright to me as per the right align display,rest strings could be modulated as per requirement. Try this :

public static void main(String[] args) {
            double total = 5000;
            double payWith = 10000;
            double change = 5000;
            Locale locale = new Locale("EN", "US");
            NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
            System.out.println("-----------------------------------------");
            System.out.println("TOTAL:\t\t\t" + formatAlignToRight(formatter.format(total)));
            System.out.println("PAY WITH:\t\t" + formatAlignToRight(formatter.format(payWith)));
            System.out.println("CHANGE:\t\t\t" + formatAlignToRight(formatter.format(change)));
            System.out.println("-----------------------------------------");
            System.out.println("      Thank you, have a good day...!!");
            System.out.println("=========================================");
        }

// To convert a print stream to string from Redirect console output to string in java

public static String formatAlignToRight(String x){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        PrintStream old = System.out;
        System.setOut(ps);
        System.out.printf("%20s",x); //This is right aligning for 20 characters
        System.out.flush();
        System.setOut(old);
        return baos.toString();
    }

Hint : You can't simply use another S.out within one for the required formatting.

Community
  • 1
  • 1
Naman
  • 27,789
  • 26
  • 218
  • 353