함수 zfill()

1 개요[ | ]

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

2 C++[ | ]

C++
Copy
#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
}
Loading

3 Excel[ | ]

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

4 Java[ | ]

Java
Copy
public class MyClass {
    public static void main(String args[]) {
        int num = 83;
        System.out.println( String.format("%04d", num) ); // 0083
    }
}
Loading
Java
Copy
public class MyClass {
    public static void main(String args[]) {
        String str = "83";
        System.out.println( String.format("%04d", Integer.parseInt(str)) ); // 0083
    }
}
Loading
Java
Copy
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
    }
}
Java
Copy
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[ | ]

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

6 Go[ | ]

Go
Copy
package main

import "fmt"

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

7 PHP[ | ]

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

8 Python[ | ]

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

9 Perl[ | ]

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

10 같이 보기[ | ]