How to return the last non zero digit of a float value (which were limited to a certain number of decilmals)

tincan

Im working on geological datasets and am trying to smooth the data distribution to improve the machine learning algorithms.

I need to calculate the error on certain values by using half of their last decimal, just like most measuring instruments recommend.

Here are some examples of what I would like to obtain from certain numbers:

0.010 -> 0.005
0.020 -> 0.005
0.090 -> 0.005
0.100 -> 0.05
0.600 -> 0.05
0.900 -> 0.05

Cheers!

studentbrad

Convert the value to a string. Count the zeros and use '0.' + num_zeros * '0' + '5' to get the new number as a string. Convert the string to a float.

def half_of_last_non_zero_digit(num):
    return float('0.' + '0' * len(str(num).split('.')[1]) + '5')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get Last digit of number

In pandas, how to replace all zero values with the last non zero value which is not more than 4 rows up, without a for loop?

How to close vim editor with non-zero return value?

How to return the last value in a vector that met a certain condition

How to get first non zero value and last non zero value indexes in array?

python function return first non zero strip middle zeros and return last part of number string

How to get the first non zero / non null value and the last zero / non null value among multiple columns using oracle sql

Regexp to validate ip address with last digit as non zero

How to add zero to single digit number(time)?

How to get the column name of last non zero value in pandas?

Excel Changing The Last Digit Of My Number To Zero

ansible how to get last 2 digit of number

How to get the int value last digit of the year?

Extract line number of a file which is having a non zero value before a specified string

How to generate but exclude a certain number in a certain digit integer in python

Use list comprehension to return number of integers which contain a digit

How to find the second to last digit of a six digit number?

Round Float Value without increasing last digit Objective C iOS

how to remove the last digit in a list if the list contain number last number some were in the list

How to find every number that a certain digit is present in

Last Non-Zero Value in Array

Return the last non-zero value in a panda df

How do you find the last non-zero number in an array?

How to generate 16 digit number with certain prefix

how to display last x float number value in chartjs

Proof that the last non-zero digit to the right of the decimal point in a non whole number float is always 5?

Formula to find the column number of last non-zero number in a row?

Building a function which return the last numeric value of a DF returns : 'float' object has no attribute 'isnumeric'

Count number of zeros after last non-zero value per row