How can I convert an Int to a 7-character long String, so that 123 is turned into "0000123"?
How can I convert an Int to a 7-character long String , so that
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The Java library has pretty good (as in excellent) number formatting support which is accessible from StringOps enriched String class:
Edit post Scala 2.10: as suggested by fommil, from 2.10 on, there is also a formatting string interpolator (does not support localisation):
Edit Apr 2019:
0from the format specifier. In the above case, it’d bef"$expr%7d".Tested in 2.12.8 REPL. No need to do the string replacement as suggested in a comment, or even put an explicit space in front of7as suggested in another comment.s"%${len}d".format("123")