2020-08-26 14:47:36 +02:00
|
|
|
import math
|
2020-08-31 23:16:07 +02:00
|
|
|
from common import inputTypeCheck
|
2020-08-26 14:47:36 +02:00
|
|
|
|
|
|
|
# Bump the decimal point up by numberOfDecimal points,
|
|
|
|
# add 0.5 to make floor go from 0-1 to 0.5-1.5,
|
|
|
|
# then add back the decimal points.
|
|
|
|
def myRoundFunction(number, numberOfDecimals):
|
|
|
|
decimalFactor = 10 ** numberOfDecimals
|
|
|
|
return math.floor(number * decimalFactor + 0.5) / decimalFactor
|
|
|
|
|
|
|
|
def removeEmptyDecimals(number):
|
|
|
|
hasEmptyDecimals = (number == int(number))
|
|
|
|
return int(number) if hasEmptyDecimals else number
|
|
|
|
|
|
|
|
number = inputTypeCheck(
|
|
|
|
message='Gi inn et desimaltall: ',
|
|
|
|
type=float,
|
|
|
|
errorMessage='Beklager, det du skrev inn er ikke et nummer. Prøv igjen\n'
|
|
|
|
)
|
|
|
|
|
|
|
|
numberOfDecimals = inputTypeCheck(
|
|
|
|
message='Antall desimaler i avrunding: ',
|
|
|
|
type=int,
|
|
|
|
errorMessage='Beklager, det du skrev inn er ikke et heltall. Prøv igjen\n'
|
|
|
|
)
|
|
|
|
|
|
|
|
roundedNumber = removeEmptyDecimals(myRoundFunction(number,numberOfDecimals))
|
|
|
|
print(f'Avrundet til {numberOfDecimals} desimal: {roundedNumber}')
|