Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:01:55+00:00 2026-05-26T20:01:55+00:00

How can I convert an Int to a 7-character long String , so that

  • 0

How can I convert an Int to a 7-character long String, so that 123 is turned into "0000123"?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:01:56+00:00Added an answer on May 26, 2026 at 8:01 pm

    The Java library has pretty good (as in excellent) number formatting support which is accessible from StringOps enriched String class:

    scala> "%07d".format(123)
    res5: String = 0000123
    
    scala> "%07d".formatLocal(java.util.Locale.US, 123)
    res6: String = 0000123
    

    Edit post Scala 2.10: as suggested by fommil, from 2.10 on, there is also a formatting string interpolator (does not support localisation):

    val expr = 123
    f"$expr%07d"
    f"${expr}%07d"
    

    Edit Apr 2019:

    • If you want leading spaces, and not zero, just leave out the 0 from the format specifier. In the above case, it’d be f"$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 of 7 as suggested in another comment.
    • If the length is variable, s"%${len}d".format("123")
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I convert string into integer and remove every character from that change.
How can I convert a string of bytes into an int in python? Say
I try to convert a unsigned long into a character string, appending a comma
How can I convert an ASCII character into an int in C?
I'm looking for a way to convert a string that contains a character escape
I'm trying to write a program that converts a long into a string and
I am attempting to convert a string into a number by summing the int
Hey, I'm looking to convert a int that is inputed by the user into
You can convert a negative number to positive like this: int myInt = System.Math.Abs(-5);
How can i convert a vartiables value int anothers name in C++? like in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.