Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Padding decimals in Java

Name: Anonymous 2013-02-03 4:44

How can I pad a double with both trailing zeros (two places, for money) and leading spaces before the $ to align the decimal places?
I want my list to look like:

$###.##
$###.##
_$##.##
__$#.##
__$0.##
__$0.00

As the value decreases. The underscores should be spaces, of course. I wasn't sure if they would appear or be deleted in the post.

Also, I changed a function somewhere and a series of print functions decided to start tabbing every fucking word so they're all spread out and I can't find anything wrong with the code. Any ideas?

System.out.print("\n\nWhat    would    you like    to    do?\n1. Calculate    the Monthly    Payment for    a fixed rate, fixed term loan.\n");
            System.out.print("2.    Calculate the number    of    Months to pay off    a loan with    a fixed monthly payment.\n");
            System.out.print("\nPlease    enter    the number of your selection:    ");

Name: Anonymous 2013-02-03 4:52

>>2

The number of digits occupied by the whole part of the number will be the floor of the log based 10 of the number. You could calculate that, print the spaces, and then print the number, or you could first print the number to a buffer and remember how many chars were printed.

Stop using java is inferior. use the return value from snprintf.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List