-3

I need help, I have an int num = 0000; but on System.out.print(num); it only shows like this: 0. I would like to know what can I do if I have num = 0111; or num = 0011; or num = 0001 and I want to show the 0 before the number, so the final result can be 0001 and not 1. Thanks!

GGarriga
  • 25
  • 1
  • 2
  • 7

3 Answers3

0

If you want to print an int formated like this 0000, you have to do this:

int num = 23;
System.out.print(String.format("%04d", num));
// result -> "0023"

Best regards

thibsc
  • 3,747
  • 2
  • 18
  • 38
0

Use printf instead:

System.out.printf("%04d", num);
peterremec
  • 488
  • 1
  • 15
  • 46
0

Kindly use String datatype instead of int. Because in Java int automatically removes extra leading zeros.

Example: String num="0000";

As you know in integers Leading zeros have no impact. From you input I think you are storing binary integers. If you want to store binary input to Integer kindly take a look on this answer.

Use Binary in Int variable in Java

Thanks

Community
  • 1
  • 1
Hammad Sajid
  • 312
  • 1
  • 3
  • 14