함수 zfill()

1 개요[ | ]

leading zero
leading 0
ZEROFILL
fill zero
  • Left padding a String with Zeros
  • Example: zfill(83, 4) → 0083

2 C++[ | ]

#include <iostream>
using namespace std;

string zfill(string s, int len) {
    return string(len-s.length(), '0')+s;
}

int main() {
 	cout << zfill("83", 4); // 0083
}

3 Excel[ | ]

'0083
="0083"
=TEXT(83,"0000")
=TEXT(83, REPT("0",4))
=REPT("0",4-LEN(A1))&A1

4 Java[ | ]

public class MyClass {
    public static void main(String args[]) {
        int num = 83;
        System.out.println( String.format("%04d", num) ); // 0083
    }
}
public class MyClass {
    public static void main(String args[]) {
        String str = "83";
        System.out.println( String.format("%04d", Integer.parseInt(str)) ); // 0083
    }
}
import org.apache.commons.lang3.StringUtils;
public class MyClass {
    public static void main(String args[]) {
        int num = 83;
        System.out.println( StringUtils.leftPad(String.valueOf(num), 4, "0") ); // 0083
    }
}
import org.apache.commons.lang3.StringUtils;
public class MyClass {
    public static void main(String args[]) {
        String str = "83";
        System.out.println( StringUtils.leftPad(str, 4, "0") ); // 0083
    }
}

5 JavaScript[ | ]

s = '83';
console.log( s.padStart(4, '0') ); // "0083"
s = 83;
console.log( s.toString().padStart(4, '0') ); // "0083"

6 Go[ | ]

package main

import "fmt"

func main() {
	fmt.Printf("%04d\n", 83)
}

7 PHP[ | ]

$num = 83;
$zero_num = str_pad($num, 4, '0', STR_PAD_LEFT);
echo $zero_num; # 0083
function zfill($num, $length) { return str_pad($num, $length, '0', STR_PAD_LEFT); }
$num = 83;
$zero_num = zfill($num, 4);
echo $zero_num; # 0083
$num = 83;
$zero_num = sprintf('%04d', $num);
echo $zero_num; # 0083

8 Python[ | ]

print( "83".zfill(4) )
for x in range(3):
    print( str(x).zfill(4) )

9 Perl[ | ]

printf("%04d\n", 83); # 0083

10 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}