a method of manipulating a number by the last digit

Asked 2 years ago, Updated 2 years ago, 27 views

a=43.8664→a=43.8665
b=45.058333333→b=45.058333334

Is there a way to manipulate only the last digit number even if the digits are different as shown above?
I've come up with a way to get the number of decimal places in a string and only +1 the last digit, but
Could you please let me know if there is anything I can do with a library?

python

2022-09-30 21:31

1 Answers

Is it correct to say that decimal numbers are given as character strings instead of being given as numerical values, and that only one decimal number is added with the accuracy expressed in the string and returned as a string?In that case, I think you can use decimal.It looks like the following.

 from decimal import Decimal, Context

def last_up(num_s):
    num=Decimal(num_s)
    return str(num.next_plus(Context(len(num.as_tuple().digits)))))

print(last_up('43.8664'))#=>43.8665
print(last_up('45.0583333'))#=>45.058333334
print(last_up('42.320000'))#=>42.320001


2022-09-30 21:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.