
풀이
C
%lf로 double 값을 입력받아 계산합니다. 조건에 맞는 소수점 형태와 달러 기호에 유의하세요. %.2lf 형태로 출력하면 소수점 아래 3번째에서 반올림하여 2번째 자리까지 출력합니다.
C++
조건에 맞는 소수점 형태와 달러 기호에 유의하세요. cout<<fixed, cout.precision(2)로 소수점 아래 3번째에서 반올림하여 2번째 자리까지 출력합니다. cout.precision()은 소수점 위 자리를 포함한 출력의 전체 길이를 정하는 함수이므로, 적용 범위를 소수점 아래만으로 한정하는 cout<<fixed 없이 단독으로 사용하면 원하는 출력이 나오지 않습니다.
Java
조건에 맞는 소수점 형태와 달러 기호에 유의하세요. String.format(“%.2f”, ) 형태로 출력하면 소수점 아래 3번째에서 반올림하여 2번째 자리까지 출력합니다.
Python
조건에 맞는 소수점 형태와 달러 기호에 유의하세요. ‘{:.2f}’.format() 형태로 출력하면 소수점 아래 3번째에서 반올림하여 2번째 자리까지 출력합니다.
코드
C
1 2 3 4 5 6 7 8 9 10 11 | #include <stdio.h> int main(){ int n, i; double price; scanf ( "%d" , &n); for (i = 0; i < n; i++){ scanf ( "%lf" , &price); printf ( "$%.2lf\n" , price*0.8); } return 0; } |
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> using namespace std; int main(){ int n; double price; cin >> n; for ( int i = 0; i < n; i++){ cin >> price; cout << fixed; cout.precision(2); cout << "$" << price*0.8 << endl; } return 0; } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.Scanner; import java.lang.String; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); double price; for ( int i = 0 ; i < n; i++){ price = sc.nextDouble(); System.out.println( "$" + String.format( "%.2f" , price* 0.8 )); } } } |
Python
1 2 3 | for _ in range ( int ( input ())): price = round ( float ( input ()) * 0.8 , 2 ) print ( "${:.2f}" . format (price)) |