Coding/Python

[python]소수점 자리수 round

후__아 2024. 4. 24. 14:25
round(값): 정수로 반올림
round(값, 자리수n): 소수점 n자리수에서 반올림 
# 프로그래머스 lv0 두 수의 나눗셈
# 테스트 케이스 실패...
def solution(num1, num2):
    return round((num1/num2)*1000)
    # 수정 후
    return int((num1/num2)*1000)

 

※ int와 round

round: 반올림
int: 정수로의 변환 - 소수점 이하 버림

 

※ format, f-string

"문자열 {1:.2f} ".format(실수1, 실수2) => 실수2를 소수점 둘째자리까지 출력

f"문자열 {변수:.3f}" => 변수의 값을 소수점 셋째자리까지 출력

 

cf) https://blockdmask.tistory.com/534

https://www.codeit.kr/community/questions/UXVlc3Rpb246NjA2MDNiOWRiMWM2OTM3NmMxM2Q5Zjlk

 

 

'Coding > Python' 카테고리의 다른 글

[python]isdigit()  (0) 2024.06.13
[python]딕셔너리, enumerate  (0) 2024.04.24
[python]리스트 합집합/교집합/차집합  (0) 2024.04.24
[python]range  (0) 2024.04.24
[python]list comprehension, if-elif-else  (0) 2024.04.24