• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DecimalFormat Mask Help

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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*/
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic