programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# DecimalFormat Mask Help

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*/

Ranch Hand
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
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
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
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.

 There are 10 kinds of people in this world. Those that understand binary get this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS