mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Added decimal_isolate.py (#3700)
* Add files via upload * Delete decimal_isolate.py * Added decimal_isolate file. * Update decimal_isolate.py * Update decimal_isolate.py * Update decimal_isolate.py * Update decimal_isolate.py * Update decimal_isolate.py * Delete decimal_isolate.py * Add files via upload * Update maths/decimal_isolate.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update decimal_isolate.py * Update decimal_isolate.py * Update decimal_isolate.py * Update decimal_isolate.py Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
c83ecacc31
commit
a538989939
45
maths/decimal_isolate.py
Normal file
45
maths/decimal_isolate.py
Normal file
@ -0,0 +1,45 @@
|
||||
"""
|
||||
Isolate the Decimal part of a Number
|
||||
https://stackoverflow.com/questions/3886402/how-to-get-numbers-after-decimal-point
|
||||
"""
|
||||
|
||||
|
||||
def decimal_isolate(number, digitAmount):
|
||||
|
||||
"""
|
||||
Isolates the decimal part of a number.
|
||||
If digitAmount > 0 round to that decimal place, else print the entire decimal.
|
||||
>>> decimal_isolate(1.53, 0)
|
||||
0.53
|
||||
>>> decimal_isolate(35.345, 1)
|
||||
0.3
|
||||
>>> decimal_isolate(35.345, 2)
|
||||
0.34
|
||||
>>> decimal_isolate(35.345, 3)
|
||||
0.345
|
||||
>>> decimal_isolate(-14.789, 3)
|
||||
-0.789
|
||||
>>> decimal_isolate(0, 2)
|
||||
0
|
||||
>>> decimal_isolate(-14.123, 1)
|
||||
-0.1
|
||||
>>> decimal_isolate(-14.123, 2)
|
||||
-0.12
|
||||
>>> decimal_isolate(-14.123, 3)
|
||||
-0.123
|
||||
"""
|
||||
if digitAmount > 0:
|
||||
return round(number - int(number), digitAmount)
|
||||
return number - int(number)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(decimal_isolate(1.53, 0))
|
||||
print(decimal_isolate(35.345, 1))
|
||||
print(decimal_isolate(35.345, 2))
|
||||
print(decimal_isolate(35.345, 3))
|
||||
print(decimal_isolate(-14.789, 3))
|
||||
print(decimal_isolate(0, 2))
|
||||
print(decimal_isolate(-14.123, 1))
|
||||
print(decimal_isolate(-14.123, 2))
|
||||
print(decimal_isolate(-14.123, 3))
|
Loading…
Reference in New Issue
Block a user