-1

I'm in my first programming class using Java, and I was told that this site is a good source for programming help.

I'm in a bit of a hard spot right now, because I'm having trouble putting numbers in U.S. currency and also aligning them by their decimal point. The assignment itself has the user entering an initial deposit and an interest rate. The program then stops when your initial deposit has doubled. Everything is working as expected, but I can't get the decimals to align.

 while(Total < Deposit*2 )
            {
                Total = Total*(InterestRate/100+1.00);
                Year = (Year + 1);
                System.out.printf("%-16s %-24s %-15s\n", Year + ".", fmt.format((Total-Deposit)), fmt.format(Total));
            }

Spacing and putting it into the currency format isn't an issue, but as of right now it aligns the dollar sign. I personally think it looks better, but the professor said he would like the decimal points aligned and now I'm dead set on figuring it out.

I've seen

String.format("%8.3f", number)

But I don't know how to get it incorporate it into my current code.

Blayze
  • 1
  • 1
  • 1
    this https://stackoverflow.com/questions/3075743/usd-currency-formatting-in-java and this https://stackoverflow.com/questions/16946694/how-do-i-align-the-decimal-point-when-displaying-doubles-and-floats answers should help you. – George Weekson Oct 11 '19 at 17:47
  • @Madstuffs I changed the variables to your options, it also looks better! – Blayze Oct 11 '19 at 18:24
  • @GeorgeWeekson I saw that post, but I was having trouble incorporating spacing, putting it in US currency, and aligning by the decimal. I can get the first two, but not all three :( – Blayze Oct 11 '19 at 18:26

2 Answers2

0

Remove the fmt.format and use the new format,

instead of:

fmt.format((Total-Deposit))

just use:

Total - Deposit

And change your format from %-24s to %8.3f

's' formats Strings and 'f' formats float numbers

egallardo
  • 1,124
  • 1
  • 13
  • 25
  • Oh, okay, that makes sense. The only thing about that option is the fmt.format puts it into currency form and the %-24s spaces them out because they have to go in a column below a header. So if I changed the string formatting they'd all be clumped together again. – Blayze Oct 11 '19 at 18:31
  • @Blayze Also take a look at this post if you want to remove the currency symbol. https://stackoverflow.com/questions/8658205/format-currency-without-currency-symbol and use the formatted string instead. – egallardo Oct 11 '19 at 19:30
0

Formatting depends on how long your Double value can be. For example, for some random value of total, deposit etc. Part of the result is shown below. (Not sure whether this is what was expected):

 5.      -18950.0000000000000    1050.000000000000
 6.      -18897.5000000000000    1102.500000000000
 7.      -18842.3750000000000    1157.625000000000
 8.      -18784.4937500000000    1215.506250000000
 9.      -18723.7184375000000    1276.281562500000
10.      -18659.9043593750000    1340.095640625000
11.      -18592.8995773437500    1407.100422656250

I used below code for this,

while(total < deposit*2 ) {
    total = total*(interestRate/100+1.00);
    year = (year + 1);
    System.out.printf("%3s \t %19.13f \t %17.12f\n", year + ".", total - deposit, total);
}

Here, in 19.13f 19 refers to the total length of the number including decimal places whereas 13 refers to only decimal part.

Madstuffs
  • 322
  • 2
  • 11
  • That works! And is also exactly how it's supposed to be formatted. The only issue is that it takes it out of the currency format that I need :/. I'm unsure how to use the Locale for US currency and do what you did because if I do I get an error. (I think because at that point it's no longer a float but a string)? – Blayze Oct 11 '19 at 21:28