• Post Reply Bookmark Topic Watch Topic
  • New Topic

DecimalFormatting to remove decimal point  RSS feed

 
Richard Cook
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Hope you are having a good day.

I am trying to format a decimal value to 1 decimal place by also remove the decimal point, but... it also needs to have trailing zeros, as follows.

Value | needed output



I have tried the following:



To no avail.

Does anyone have any ideas?


Regards



Richard
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Configure the formatter to not print fractional part of the numbers at all. Multiply the value by 10000 before printing the value.
 
Richard Cook
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ya,

Thanks for that, I tried this but I think I may have got the right idea, it still leaves the decimal point on:




Cheers


Richard
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've overlooked the Double.valueOf(...).toString() in your code. You need to drop this. Just return the output of the formatter from your method.

(You're converting the formatter's output back to Double and then to String again, which imposes further unwanted conversions.)
 
Peter van de Riet
Ranch Hand
Posts: 112
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The statement stringValue.replaceAll(".", ""); does not change the immutable String. In stead you could use:

 
Richard Cook
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, that worked a treat :-)

Although when I did:



It returned a blank string.

I settled with:




Thanks again!
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter van de Riet wrote:The statement stringValue.replaceAll(".", ""); does not change the immutable String. In stead you could use:


Yes, good catch.

However, the decimal separator is introduced by the unwanted String to Double to String conversion. If the value is formatted without the fractional part, as I've suggested, there is no need to remove the decimal separator at all. (Other solutions are of course possible.)
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard, it looks ok now. However, the stringValue.replaceAll(".", ""); statement in your latest code does nothing and you could (should) remove it.
 
Richard Cook
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh so it does, even better, thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!