• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Remove leading zeros up to "0.xx"

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given a String like "000864.50", how can I strip off the leading zeros except if it's the last character before the decimal? The strings will always end in a decimal place and two digits.

So:

000756.90 becomes 756.90, and 0000070.50 should become 70.50

It seems like I want to do two things:
1. replace all instances of "00." with "0.", recursively, in case it's all leading zeros, and
2. remove all zeros that occur before a pattern that begins with a non-zero digit and ends with a decimal
 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(String.format("%.2f",Float.valueOf("000001.20")));
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see how that removes them from the string. All that does is use a format mask to print the string without them...but they are still there.
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, String is immutable so you can't change it in any way. So what's really needed is a way of producing a new string with the desired characteristics. And that's what Don Redd's code does (I suppose -- haven't checked it). Of course in real life you would assign the result of that expression to a variable rather than just printing it, which wasn't what was asked for.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get all that...

My veiled point was that the solution given didn't really solve the problem as stated...The OP really needs to clarify what they want, if nothing else as an exercise in writing clear specs.
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic