How to convert an Int to a String of a given length with leading zeros to align?

前端 未结 7 1279
醉酒成梦
醉酒成梦 2020-12-12 16:40

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

相关标签:
7条回答
  • 2020-12-12 17:19

    The padding is denoted by %02d for 0 to be prefixed to make the length 2:

    scala> val i = 9 
    i: Int = 9
    
    scala> val paddedVal = f"${num}%02d"
    paddedVal: String = 09
    
    scala> println(paddedVal)             
    09
    
    0 讨论(0)
  • 2020-12-12 17:22

    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 讨论(0)
  • 2020-12-12 17:25

    Do you need to deal with negative numbers? If not, I would just do

    def str(i: Int) = (i % 10000000 + 10000000).toString.substring(1)
    

    or

    def str(i: Int) = { val f = "000000" + i; f.substring(f.length() - 7) }
    

    Otherwise, you can use NumberFormat:

    val nf = java.text.NumberFormat.getIntegerInstance(java.util.Locale.US)
    nf.setMinimumIntegerDigits(7)
    nf.setGroupingUsed(false)
    nf.format(-123)
    
    0 讨论(0)
  • 2020-12-12 17:27

    Short answer:

    "1234".reverse.padTo(7, '0').reverse
    

    Long answer:

    Scala StringOps (which contains a nice set of methods that Scala string objects have because of implicit conversions) has a padTo method, which appends a certain amount of characters to your string. For example:

    "aloha".padTo(10,'a')
    

    Will return "alohaaaaaa". Note the element type of a String is a Char, hence the single quotes around the 'a'.

    Your problem is a bit different since you need to prepend characters instead of appending them. That's why you need to reverse the string, append the fill-up characters (you would be prepending them now since the string is reversed), and then reverse the whole thing again to get the final result.

    Hope this helps!

    0 讨论(0)
  • 2020-12-12 17:28

    huynhjl beat me to the right answer, so here's an alternative:

    "0000000" + 123 takeRight 7
    
    0 讨论(0)
  • 2020-12-12 17:33

    In case this Q&A becomes the canonical compendium,

    scala> import java.text._
    import java.text._
    
    scala> NumberFormat.getIntegerInstance.asInstanceOf[DecimalFormat]
    res0: java.text.DecimalFormat = java.text.DecimalFormat@674dc
    
    scala> .applyPattern("0000000")
    
    scala> res0.format(123)
    res2: String = 0000123
    
    0 讨论(0)
提交回复
热议问题