Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

'Implied Decimal' format in Java for Double/Float  RSS feed

 
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.
 
Marshal
Posts: 61710
193
  • 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
Marshal
Posts: 61710
193
  • 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.
 
Ranch Hand
Posts: 3171
18
  • 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:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!