Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing out 3 of numbers in java

The output value is 100000 to 10 random natural numbers
What should I do to print out only from the right end to the third?
And if you print it out with multiple of ten, an error appears
so, if we get a multiple of 10, i want to put out 010 something

for example, input -> 6545 output -> 545,
input -> 27 output -> 027

and i’m using java.
pls help me

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming your inputs are actually ints:

System.out.printf("%03d",inputvalue%1000); 

should give the result you want:

  • "%03d" forces output with zero padding
  • inputvalue%1000 calculates the remainder of division by 1000 (modulo), which will chop off all digits to the left of the three last
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading