Menu
Coddy logo textTech

String을 double로 변환

Coddy Dart 여정의 기초 섹션에 포함된 레슨 — 94개 중 84번째.

double.parse() 메서드는 10진 숫자의 문자열 표현을 double 값으로 변환합니다.

소수점 숫자를 포함하는 문자열을 생성하세요:

String priceAsString = '19.99';

double.parse()를 사용하여 문자열을 double로 변환하세요:

double price = double.parse(priceAsString);
print(price);

위의 코드를 실행한 후 출력은 다음과 같습니다:

19.99

변환된 double을 계산에 사용할 수 있습니다:

String discountAsString = '5.50';
double discount = double.parse(discountAsString);
double finalPrice = price - discount;
print('Final price: $finalPrice');

위 코드를 실행하면 출력 결과는 다음과 같습니다:

Final price: 14.49
challenge icon

챌린지

초급

이 챌린지에서는 double.parse()를 사용하여 문자열을 double로 변환하는 연습을 합니다. 이 메서드는 소수점 숫자의 문자열 표현을 double 값으로 변환합니다.

priceText 문자열을 double로 변환하여 priceValue 변수에 저장하도록 코드를 완성하세요.

치트 시트

double.parse() 메서드는 10진 부동소수점 숫자의 문자열 표현을 double 값으로 변환합니다.

String priceAsString = '19.99';
double price = double.parse(priceAsString);
print(price); // Output: 19.99

변환된 double 값을 계산에 사용할 수 있습니다:

String discountAsString = '5.50';
double discount = double.parse(discountAsString);
double finalPrice = price - discount;
print('Final price: $finalPrice'); // Output: Final price: 14.49

직접 해보기

void main() {
  // 이 문자열은 가격을 텍스트로 포함합니다
  String priceText = "23.50";
  
  // TODO: double.parse()를 사용하여 priceText를 double로 변환하세요
  // 그리고 priceValue 변수에 저장하세요
  double priceValue;
  
  // 이것은 세금(15%)을 포함한 가격을 계산합니다
  double priceWithTax = priceValue * 1.15;
  
  // 이것은 결과를 표시합니다
  print("Original price: \$${priceText}");
  print("Price as a double: ${priceValue}");
  print("Price with 15% tax: \$${priceWithTax.toStringAsFixed(2)}");
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

기초의 모든 레슨