Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

'Implied Decimal' format in Java for Double/Float

 
Haq Yunus
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a very simple question.

I want to know that if there is a format class, method or any wy to format a decimal number into a implied decimal format?

Some examples, for implied decimal to 2 places:

input: 12345.06
output: 1234506

input: 12345.00
output: 1234500

input: -152345.45
output: -15234545

input: -152345.457892
output: -15234546

input: 12.16223
output: 1216



I can handle the rounding, alignment, the negative sign and fixed length for the number but not this implied decimal thingy. I have looked at DecimalFormat but I couldn't find anything that does this. Also, nothing in String.format. Currently I am using a sort of a workaround that I first convert the decimal number into a String of my desired format (precision, size, padding etc.) and then through String manipulation create an implied decimal string. Am I missing something basic here?

Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[campbell@campbell1110 java]$ java DecimalDemo 123.456789
Your value is 123.456789, multiplied by 100 you get 12346 and as an int 12346
 
Haq Yunus
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the idea. But you think there is no support from the formatting perspective in shape of class, method, pattern or API?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is formatting support. It is particularly unusual to need to round a number and multiply by 100 simultaneously, and you can't expect a standard library to provide that function.
 
Mike Simmons
Ranch Hand
Posts: 3036
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I've never heard of "implied decimal" before now - I doubt there's any need for a standard library. Especially when it's really, really easy to do it yourself:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic