• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatting a String to a decimal  RSS feed

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a value that is 6 characters that I want to convert to a 4 position decimal value. For some reason, my little pea brain cannot figure out how to do this! For example, I have the value "000001". I want to be able to convert this string to 00.0001. I've tried a couple of things with DecimalFormat, but still haven't gotten the result I'm looking for. Can anyone give me a push in the right direction?
Thanks!
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want an integer padded with left zeros of length six as a string and to parse that into a four–place decimal formatted to have two leading zeros. DecimalFormat will try to make sense of the numbers.
This looks like something we can do with a String format method.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!