"String을 int로 변환"의 두 판 사이의 차이

98번째 줄: 98번째 줄:
==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<syntaxhighlight lang='perl'>
<syntaxhighlight lang='perl' run>
print 0 + '42';                       # 42
print 0 + '42';       # 42
print int '42,000';                   # 42
print int '42,000';   # 42
</syntaxhighlight>
</syntaxhighlight>



2021년 4월 13일 (화) 21:46 판

  다른 뜻에 대해서는 정수형 문서를 참조하십시오.
string-int 형 변환
String to int
String to integer
atoi
parseInt
Integer.parseInt

1 Bash

printf '%d\n' "42" 2>/dev/null           # 42
printf '%d\n' "42,000" 2>/dev/null       # 42

2 C

#include <stdio.h>
#include <stdlib.h>
int main() {
    char *str = "123";
    printf("%d\n", atoi(str)); // 123
    return 0;
}

3 C++

#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
	string str = "123";
	int n = atoi(str.c_str());
	cout << n << endl;
    // 123	
}
#include <iostream>
#include <sstream>
using namespace std;
int main() {
	string str = "123";
	std::stringstream ss(str);
	int n;
	ss >> n;
	cout << n << endl;
    // 123	
}

4 C#

using System;class Z{static void Main(){

string str = "123";
int i = int.Parse(str);
Console.WriteLine(i); 

}}

5 Java

class {public static void main(String[] ){

String str = "123";
int i = Integer.parseInt(str);
System.out.println(i);

}}

6 JavaScript

console.log( parseInt('123.45') );  // 123
console.log( parseInt('77') );      // 77

7 Objective-C

NSString *s = @"42";
int i = [s intValue];

8 Perl

print 0 + '42';       # 42
print int '42,000';   # 42

9 PHP

echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42

echo intval('42,000');                // 42

echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval(042);                     // 34
echo intval('042');                   // 42
echo intval(1e10);                    // 1410065408
echo intval('1e10');                  // 1
echo intval(0x1A);                    // 26

echo 0 + '42';                        // 42

var_dump( intval('hello42') );        // int(0)
var_dump( intval('42hello') );        // int(42)
function string_to_int($str) { return (int)preg_replace('/[^\-\d]*(\-?\d*).*/','$1',$str); }
var_dump( string_to_int( 'The value is 0083.123' ) );
// int(83)

10 Python

s = "123"
i = int(s)
print( type(i) )
# <class 'int'>

11 Ruby

str = "123"
i = str.to_i

12 VB

Dim s As String
Dim i As Integer

s = "42"
i = Val(s)

13 같이 보기