How to remove zero from a number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

How to remove zero from a number

a simple java program to remove all the zeroes from a given number

10th Feb 2017, 9:41 AM
Taylor
Taylor - avatar
3 Answers
+ 8
From the perspective of a C++ programmer, I'd take input as string and transfer all characters which are not '0' over to another character array, in which I will then convert to another string.
10th Feb 2017, 10:07 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
@Justin Hill no need to undermine your own efforts. The method is direct and splendid.
10th Feb 2017, 10:15 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Might not be the best way, but it's what I came up with. Basically I wrote a method that turned the Integer into a string, removed the parts of the string I didn't want, turned it back into an integer, and returned it. felt like a dirty way to do it, but it worked public static int remove(int x){ String var = String.valueOf(x); var = var.replace("0", ""); x = Integer.valueOf(var); return x; } int number = 1305603020; number = remove(number); System.out.println(number); System.out.println(remove(1010101));
10th Feb 2017, 10:11 AM
LordHill
LordHill - avatar