"함수 zfill()"의 두 판 사이의 차이

8번째 줄: 8번째 줄:
* Left padding a String with Zeros
* Left padding a String with Zeros
* Example: zfill(83, 4) → 0083
* Example: zfill(83, 4) → 0083
==JavaScript==
[[분류: JavaScript]]
{{참고|자바스크립트 padStart()}}
<syntaxhighlight lang='JavaScript' run>
s = '83';
console.log( s.padStart(4, '0') ); // "0083"
</syntaxhighlight>
<syntaxhighlight lang='JavaScript' run>
s = 83;
console.log( s.toString().padStart(4, '0') ); // "0083"
</syntaxhighlight>


==Excel==
==Excel==
74번째 줄: 62번째 줄:
         System.out.println( StringUtils.leftPad(str, 4, "0") ); // 0083
         System.out.println( StringUtils.leftPad(str, 4, "0") ); // 0083
     }
     }
}
</syntaxhighlight>
==JavaScript==
[[분류: JavaScript]]
{{참고|자바스크립트 padStart()}}
<syntaxhighlight lang='JavaScript' run>
s = '83';
console.log( s.padStart(4, '0') ); // "0083"
</syntaxhighlight>
<syntaxhighlight lang='JavaScript' run>
s = 83;
console.log( s.toString().padStart(4, '0') ); // "0083"
</syntaxhighlight>
==Go==
{{참고|Go zerofill}}
<syntaxhighlight lang='go' run>
package main
import "fmt"
func main() {
fmt.Printf("%04d\n", 83)
}
}
</syntaxhighlight>
</syntaxhighlight>

2023년 1월 29일 (일) 22:24 판

1 개요

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

2 Excel

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

3 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
    }
}

4 JavaScript

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

5 Go

package main

import "fmt"

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

6 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

7 Python

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

8 Perl

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

9 같이 보기

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