나는 이렇게 학습한다/Algorithm & SQL

1222. Formatting decimal places #0

daco2020 2022. 12. 23. 00:18
반응형

Each number should be formatted that it is rounded to two decimal places. You don't need to check whether the input is a valid number because only valid numbers are used in the tests.

Example:    
5.5589 is rounded 5.56   
3.3424 is rounded 3.34



Solution:

def two_decimal_places(n: float) -> float:
    l, r = str(n).split(".")
    r = str(round(int("1"+r[:5]), -3))
    return float(".".join([l, r[1:3]]))
def two_decimal_places(n: float) -> float:
    return round(n, 2)


반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

1224. Regex count lowercase letters  (0) 2022.12.25
1223. validate code with simple regex  (0) 2022.12.23
1221. Name Shuffler  (0) 2022.12.21
1220. The 'if' function  (0) 2022.12.20
1219. Return the day  (0) 2022.12.19