• Post Reply Bookmark Topic Watch Topic
  • New Topic

double variable and place values  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way to restrict the place value of a double variable? I'm writing a change counting program, and I'm getting a number out to the bajillionth place. For reference, here is the code



TextIO is an input/output external class I'm using, designed by David J. Eck. I doubt its the problem, but the coding for it is currently above my skill.

I ran multiple searches, but google fails me on this one guys
 
author
Marshal
Posts: 23441
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Raine Andrews wrote:is there a way to restrict the place value of a double variable?



A few options are ... (1) use the printf() method, along with appropriate formatting, (2) use NumberFormat to convert it to a string and print the string.

Henry
 
Raine RaineAndrews
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll google up those terms and see what I can learn!
 
Raine RaineAndrews
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh my... just a tad bit over my current skill
 
Raine RaineAndrews
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! I deleted the quarter, dime, nickle, and penny variables, and instead plugged in the value times 100 where the variables used to be. If I divide that at the end by 100, it gives me a nice round number ^.^
 
Henry Wong
author
Marshal
Posts: 23441
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Raine Andrews wrote:Got it! I deleted the quarter, dime, nickle, and penny variables, and instead plugged in the value times 100 where the variables used to be. If I divide that at the end by 100, it gives me a nice round number ^.^



That's actually a common solution for monetary values -- using whole numbers to represents the total number as cents, instead of using floating point to represent the dollars and cents.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!