aspose file tools*
The moose likes Java in General and the fly likes 'Implied Decimal' format in Java for Double/Float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " Watch " New topic
Author

'Implied Decimal' format in Java for Double/Float

Haq Yunus
Ranch Hand

Joined: Apr 08, 2009
Posts: 42
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

Joined: Oct 13, 2005
Posts: 39773
    
  28
[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

Joined: Apr 08, 2009
Posts: 42
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

Joined: Oct 13, 2005
Posts: 39773
    
  28
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 'Implied Decimal' format in Java for Double/Float