HOME > ゼロ埋め
数字を含む文字列の桁数を揃えるためにゼロ埋めをする必要がありましたので調査
参考:[iOS8] Swiftで数字の桁数をゼロ埋めする方法
参考:http://tech.eversense.co.jp/174
参考:Leading zeros for Int in Swift
let hoge1 = NSString(format: "%04d", 1) // => 0001
let hoge2 = NSString(format: "%04d", 12) // => 0012
let hoge3 = NSString(format: "%04d", 123) // => 0123
let hoge4 = NSString(format: "%04d", 1234) // => 1234
let hoge5 = NSString(format: "%0.4f", 0.123) // => 0.1230
let hoge6 = NSString(format: "%09.4f" , 12.34) // => 0012.3400
NSStringを駆使すれば実施できそうです。引数のformat部分を上記の様に微調整すればゼロで埋めることができます!
先頭の9は表示桁数(小数点含む)で後ろの4は小数点以下になります。最後のfやdは整数だったり小数点が含まれる場合に それぞれ変更して利用する必要があります。上記であれば整数の場合はd、小数点を含む数の場合はfが 利用されます。
プログラミングしていると時々この桁数を合わせる必要が出てくる時があります。そんな時にぜひ思い出していただければ感謝です!
2021-05-14 14:21:41 | The pitfalls of Filetransfer in watchconnectivity on WatchOS. And how to avoid.
AppleWatch it succeeds with the actual machine, but if it is a simulator, it will fail、、、
When I... |
2021-05-06 14:04:37 | Line's animation stamp production pitfalls 、、、 failure story
I tried to make a LINE sticker during Golden Week.
I made it and applied, but it was different f... |
2021-04-22 14:54:53 | Cool carousel carefully selected 3 points! Astringent image slider using WebGL.
I was on a trip to find a cool slider yesterday.
It's simple, easy to use, and has a good de... |
Tweet
![]() |
|||
|