Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

Mike MacDonald
Greenhorn
Posts: 5
//I am trying to output a percent value to look like this:
41.7%

//but my output looks like this:
4166.7%

//here is my attempt at a formatted mask:

DecimalFormat msk1=new DecimalFormat("##.0%");

//and here is my code using the mask:

System.out.println("Percent Females:"+" "+msk1.format(percentGirls));

/*My instructor didn't really explain the ins and out of proper format masking syntax so this is my newbie guess. Any help and insight on proper formatting would be awesome*/

Ray Stojonic
Ranch Hand
Posts: 326

The bad news: Your algorithm is not doing what you are expecting it to do. Try outputting the value of percentGirls without formatting it to see what I mean.

Mike MacDonald
Greenhorn
Posts: 5
Originally posted by Ray Stojonic:

The bad news: Your algorithm is not doing what you are expecting it to do. Try outputting the value of percentGirls without formatting it to see what I mean.

Mike MacDonald
Greenhorn
Posts: 5
Originally posted by Ray Stojonic:

The bad news: Your algorithm is not doing what you are expecting it to do. Try outputting the value of percentGirls without formatting it to see what I mean.

//Here is my output without the mask:

Percent Females: 41.66666666666667

//Heres the algorithm:

//percentages by gender
double percentGirls=(((double)totalGirls/totalStudents)*100);

System.out.println("Percent Females:"+" "+percentGirls);

Ray Stojonic
Ranch Hand
Posts: 326
You're getting what you ask for, you just don't want it.

the mask "##.0%" says: number(hide zero), number(hide zero) dot number(don't hide zero) show as percentage after multiplying by 100

You want to drop either the multiplcation in your algo, or in your mask. Right now, you're multiplying by 100 twice.