• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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: 13073
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: 27371
88
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: 13073
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.
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic