Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework
this week in the
Java in General
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
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
Java in General
Removing zeros in front of a string
igwe kalu kalu ogba
Ranch Hand
Posts: 133
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi, I have a question:
I'm retrieving values from a database and getting a number like 23 outputted as 00000024, It varies.
My question is, how can I remove the first x zeros from a number. Can any of the numberformat tools help? Thanks.
Cathy Song
Ranch Hand
Posts: 92
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Something like this perhaps?
class a{
public static void main (
String
args[]){
String
test
= "0000024";
Integer i = new Integer (test);
System.out.println (i);
int num = i.intValue();
System.out.println (num);
}
}
Stefan Wagner
Ranch Hand
Posts: 1923
I like...
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I guess he means only to suppress some of the Zeros:
000000042
....00042 (x=4).
printf or regexp could help:
public void printfTrim (String number, int x) { int len = number.length () - x; String formatString = "Number %1$s with %2$d 0s suppressed: %3$0" + len + "d\n"; int value = Integer.parseInt (number); System.out.printf (formatString, number, x, value); } public void regexTrim (String number, int x) { String trun = number.replaceAll ("\\b0{" + x + "}", ""); System.out.println ("Number " + number +" with " + x + " 0s suppressed: " + trunc); }
[ August 04, 2005: Message edited by: Stefan Wagner ]
[ August 04, 2005: Message edited by: Stefan Wagner ]
[ August 04, 2005: Message edited by: Stefan Wagner ]
http://home.arcor.de/hirnstrom/bewerbung
Dharmesh Gangani
Ranch Hand
Posts: 30
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You can get the data truncated from the database query as well. this removes a few lines of code.
Eg. If you want to truncate the value from the database to say 2 leading zeros' (0024), u can use the database query as -
select to_char(<col_name>, '0099') from <table_name>
The tow 0's indicate the number of leading zero's required & the 9's indicate the fixed number of digits required.
Hope this solves your purpose.
In case you have any further queries, please feel free to ask.
-=-=-=-=-=-=-=-=-=-<br />Thanks & Regards,<br />Dharmesh G.
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Sequence Problem
String to long datatype conversion problem
XSLT format-number() issue
left filled zero
Can you convert HEX to Binary w.o. lossing leading zeros?
More...