Question:
Need to convert String which is having some number with leading zeros and need to convert it to int in java?
2009-07-31 06:11:50 UTC
Hello
I have a String which contains 0086789 and i want to convert it to int but i dont want to loose 00 in int value.I want int value same as String .
I need to set int val (need 0086789) to some int property.
How can it be done?
Thanks in @dvance
Five answers:
Neeraj Yadav♄
2009-08-02 10:56:22 UTC
Well it wouldn't be possible to have integer with 00 in the beginning of that literal. As,its not code as such for literals like integer under JVM specifications.



Possible solutions..

1)If you are particular about number of digits in that int

You might use padding technique over that integer which will

resolve your problem of appending zero's on left at any cost.



/**

* Pads a String s to take up n

* characters, padding with char c on the

* left (true) or on the right (false).

* Returns null if passed a null

* String.

**/

public static String paddingString(String s, int n, char c,

boolean paddingLeft) {

if (s == null) {

return s;

}

int add = n - s.length(); // may overflow int size... should not be a problem in real life

if(add <= 0){

return s;

}

StringBuffer str = new StringBuffer(s);

char[] ch = new char[add];

Arrays.fill(ch, c);

if(paddingLeft){

str.insert(0, ch);

} else {

str.append(ch);

}

return str.toString();

}



so you can apply technique on every integer once you are done with

int related operation over it.



2)Decimalformatter

You need to first divide the number by considered number of digits with that number of zeros

1234 will be ..1234 ie. 1234/10000



later use something like

String frmt="#0.000";

NumberFormat formatter = new DecimalFormat(frmt);

formatter.format(number)



get some more formats at

http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html





Hope this does resolves your issues

Cheers:)
Silent
2009-08-01 06:49:32 UTC
It can't, really. An int in Java is just a number; it doesn't include any information about how to visually represent that number.



Take a step back for a minute. Why do you want to convert it to an int and at the same time retain leading zeroes? If you're adamant about keeping those zeroes, then are you really using this value as a number?
shavenlunatic
2009-07-31 06:18:41 UTC
// The 0 symbol shows a digit or 0 if no digit present

NumberFormat formatter = new DecimalFormat("000000");

String s = formatter.format(-1234.567); // -001235

// notice that the number was rounded up
Joe_Young
2009-07-31 06:20:18 UTC
http://www.java-examples.com/convert-string-java-int-example
2016-04-11 06:15:04 UTC
You could try treating your number as a string initially, then iterate through each character (digit) to print its word equivalent. Maybe something like: public static void printNumbers(int i) { for(char c : ("" + i).toCharArray()) { switch(c) { case '0' : System.out.print("zero "); break; case '1' : System.out.print("one "); break; case '2' : System.out.print("two "); break; case '3' : System.out.print("three "); break; case '4' : System.out.print("four "); break; case '5' : System.out.print("five "); break; case '6' : System.out.print("six "); break; case '7' : System.out.print("seven "); break; case '8' : System.out.print("eight "); break; case '9' : System.out.print("nine "); break; default: break; } } System.out.println(); } You could also add a case for '.' if you wanted to add "decimal place" or something like that for doubles or floats.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...