aspose file tools*
The moose likes Beginning Java and the fly likes DecimalFormat Mask Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DecimalFormat Mask Help" Watch "DecimalFormat Mask Help" New topic
Author

DecimalFormat Mask Help

Mike MacDonald
Greenhorn

Joined: Jan 24, 2005
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*/


Being a newbie at any language is like panhandling. It sucks!!!
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
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

Joined: Jan 24, 2005
Posts: 5
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

Joined: Jan 24, 2005
Posts: 5
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

Joined: Aug 08, 2003
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DecimalFormat Mask Help