aspose file tools*
The moose likes Beginning Java and the fly likes double variable and place values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double variable and place values" Watch "double variable and place values" New topic
Author

double variable and place values

Raine RaineAndrews
Greenhorn

Joined: Jun 27, 2010
Posts: 9
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raine RaineAndrews
Greenhorn

Joined: Jun 27, 2010
Posts: 9
Thanks, I'll google up those terms and see what I can learn!
Raine RaineAndrews
Greenhorn

Joined: Jun 27, 2010
Posts: 9
oh my... just a tad bit over my current skill
Raine RaineAndrews
Greenhorn

Joined: Jun 27, 2010
Posts: 9
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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: double variable and place values