Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DecimalFormat Mask Help

 
Mike MacDonald
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The good news: your mask is just fine.

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ray Stojonic:
The good news: your mask is just fine.

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ray Stojonic:
The good news: your mask is just fine.

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic