Decimal number parse in Python - python

I have a number as in below:
.12
in my code I convert this number to string and parse it from the "point" to decimal as in below:
mydecimal = str(mydecimal_new)
whole_number = mydecimal.split('.')
main_number = whole_number[0]
#print(main_number)
additional_number = whole_number[1]
#print(additional_number)
My problem is when I try to convert this number to str python add 0 in front of the number and it keep the number as:
0.12
I want to know how can I avoid from the adding zero in front of this

Try This :
def _remove_leading_zero(value, string):
if 1 > value > -1:
string = string.replace('0', '', 1)
return string
class MyFloat(float):
def __str__(self):
string = super().__str__()
return _remove_leading_zero(self, string)
def __format__(self, format_string):
string = super().__format__(format_string)
return _remove_leading_zero(self, string)
print(MyFloat(.12))
You Can Fly to the Moon Now :))) Good Luck.

Related

Python return regexp-formatted string

There is an input string like "2r-rj1225-f11e-12-x-w"
The task is to return it in the following format:
all groups except the first and last must be 5 characters
the first and the last groups must be between 1 and 5 characters
if the first group in the input is less than 5 characters, it must be preserved
that results to is "2r-rj122-5f11e-12xw"
import re
string = "2r-rj1225-f11e-12-x-w"
baseLength = 5
def formatKey(string: str, baseLength: int) -> str:
p = re.compile(r"{1,baseLength}[a-zA-Z0-9]{baseLength}[a-zA-z0-9]+")
formatted = '-'.join(p.match(string))
return formatted
print(f'The reformatted string is {formatKey(string, baseLength)}')
that does not work, naturally. And I also wish to avoid '-'.join and to simply return something like regexp(re.compile('[a-z]FORMATREGEXP'), string) where FORMATREGEXP is the regexp that does the job.
Clarification: The actual solution is to use re.sub(pattern, repl, string) function: "The sub() function searches for the pattern in the string and replaces the matched strings with the replacement" -- And that is exactly what I've been asking for, that simple, in one line!!
I don't really see this as a regex problem. It's just reorganizing the characters after the first hyphen.
x = "2r-rj1225-f11e-12-x-w"
def reencode(x):
parts = x.split('-')
p1 = ''.join(parts[1:])
s = parts[0]
while len(p1) >= 5:
s += '-' + p1[:5]
p1 = p1[5:]
if p1:
s += '-' + p1
return s
print(reencode(x))
Output:
2r-rj122-5f11e-12xw

How to print an ID number include leading zeros?

I'm writing an Iterator class that generate all possible ID numbers.
the issue is that I need it to print all 9 digit numbers including the ones that starts with 0 for example: 000000001 000000002 ect.
I need the output as a number and not a string, is there a way to do it?
my code:
class IDIterator:
def __init__(self):
self._id = range(000000000, 1000000000)
self._next_id = 000000000
def __iter__(self):
return self
def __next__(self):
self._next_id += 1
if self._next_id == 999999999:
raise StopIteration
return self._next_id
ID = iter(IDIterator())
print(next(ID))
print(next(ID))
print(next(ID))
output = 1
2
3
..
Python has a built in string function which will perform the left-padding of zeros
>>> before = 1
>>> after = str(before).zfill(9)
000000001
>>> type(after)
<class 'str'>
If you need the ID returned as an integer number with leading zeros preserved, I don't believe there's a way to do what you're looking for--the primitive Python type simply does not support this type of representation. Another strategy would be to store the ID's as normal integers and format with leading zeros when you need to display something to the user.
def format_id(string, length=9):
return str(string).zfill(length)

Is there a simple and preferred way of German number string formatting in Python?

I am searching for the proper way of German number formatting (e.g. 1.000,1234) in Python under Windows OS.
I tried locale.setlocale but did not succeed.
Instead, I have written a function to come up with the desired output.
Is there a better way?
def ger_num(number, precision=3):
"""
returns german formatted number as string or an empty string
"""
if number is not None:
try:
my_number = "{:,f}".format(number)
except ValueError:
return ""
decimals, fraction = my_number.split(".")[0], my_number.split(".")[1]
decimals = decimals.replace(",", ".")
if precision:
return decimals + "," + fraction[:precision]
else:
return decimals
else:
return ""
You can use locale.setlocale to set the locale to de and then use locale.format to format your number:
import locale
locale.setlocale(locale.LC_ALL, 'de')
print(locale.format('%.4f', 1000.1234, 1))
This outputs:
1.000,1234
If for some reason locale does not work for you (or is not desired), then the easiest other option would probably be to use string replacement, like suggested in this already mentioned answer (which draws its answer from the PEP-378).
You can always encapsulate that in a function, maybe like this:
def format_number(number, precision=3):
# build format string
format_str = '{{:,.{}f}}'.format(precision)
# make number string
number_str = format_str.format(number)
# replace chars
return number_str.replace(',', 'X').replace('.', ',').replace('X', '.')
This works well for int, float and Decimal:
>>> format_number(1)
'1,000'
>>> format_number(1, 2)
'1,00'
>>> format_number(1, 7)
'1,0000000'
>>> format_number(1234567, 7)
'1.234.567,0000000'
>>> format_number(1234567.9988, 7)
'1.234.567,9988000'
>>> format_number(1234567.9988, 2)
'1.234.568,00'
>>> from decimal import Decimal
>>> format_number(Decimal('1234567.9988'), 2)
'1.234.568,00'
>>> format_number(Decimal('1234567.9988'), 5)
'1.234.567,99880'
>>> format_number(Decimal('1234567.9988'), 0)
'1.234.568'
>>> format_number(Decimal('123456'), 5)
'123.456,00000'
Thanks for the help. If anyone finds this useful I do provide the code for my final solution here.
ger_num.py:
def ger_num(number, digits=2):
'''
retruns <number> as german formattet string (thousands-.),
rounds to n <digits>.
<number> == None OR (!= INT AND != FLOAT) returns '' (empty STR)
'''
import locale
locale.setlocale(locale.LC_ALL, 'de')
if number is None:
return ''
if not isinstance(number, int) and not isinstance(number, float):
return ''
else:
format = '%.'+str(digits)+'f'
return locale.format_string(format, number, 1)
if __name__ == "__main__":
pass

How to get number of decimal places

How would I do the following:
>>> num_decimal_places('3.2220')
3 # exclude zero-padding
>>> num_decimal_places('3.1')
1
>>> num_decimal_places('4')
0
I was thinking of doing:
len((str(number) if '.' in str(number) else str(number) + '.').rstrip('0').split('.')[-1])
Is there another, simpler way to do this?
You can use a regex to parse value, capture the decimal digits and count the length of the match, if any:
import re
def num_decimal_places(value):
m = re.match(r"^[0-9]*\.([1-9]([0-9]*[1-9])?)0*$", value)
return len(m.group(1)) if m is not None else 0
this is a bit less "raw" than splitting the string with multiple if else, not sure if simpler or more readable, though.
The best and the most Pythonic way to achieve this is:
import decimal
x = '56.001000'
x = x.rstrip('0') # returns '56.001'
x = decimal.Decimal(x) # returns Decimal('0.001')
x = x.as_tuple().exponent # returns -3
x = abs(x) #returns 3
Above code can be written in simpler way as:
>>> x = '56.001000'
>>> abs(decimal.Decimal(x.rstrip('0')).as_tuple().exponent)
3
Below is the list of used functions for more reference:
str.rstrip(): Return a copy of the string with trailing characters removed.
decimal.Decimal(): Construct a new Decimal object based from value.
x.as_tuple(): Returns a namedtuple of the format: DecimalTuple(sign=0, digits=(1,), exponent=-3)
abs(): Return the absolute value of a number.
You dont need regex, you can convert to float and convert back to string! this automatically will remove the zeroes :
>>> def convertor(s):
... try :
... int(s.rstrip('0').rstrip('.'))
... return 0
... except:
... return len(str(float(s)).split('.')[-1])
...
>>> convertor('4.0230')
3
>>> convertor('4.0')
0
>>> convertor('4')
0
you could also just try something like:
try:
return len(str(num).split('.')[1].rstrip('0'))
except
return 0
By string process:
Check . is present in number string or not.
If Not present then return 0.
If present the split number string by . and get second item from the split result.
Remove 0 from the right side.
Return len of item.
code:
>>> def dCount(no_str):
... if "." in no_str:
... return len(no_str.split(".")[1].rstrip("0"))
... else:
... return 0
...
>>> dCount("2")
0
>>> dCount("2.002")
3
>>> dCount("2.1230")
3
>>> dCount("2.01230")
4
>>>
import re
def f(s):
ls = s.split('.', 1)
if len(ls) == 2 and re.match(r'\d*$', ls[1]):
return len(ls[1].rstrip('0'))
return 0
assert f('12') == 0
assert f('12.') == 0
assert f('12.1') == 1
assert f('12.100006') == 6
assert f('12.1.3') == 0
assert f('12.1abc') == 0
assert f('12.100000') == 1
The Decimal type is perfect for this, you can implement num_decimal_places() as follows:
from decimal import Decimal
def num_decimal_places(value: str):
return -Decimal(value).normalize().as_tuple().exponent
It works as follows: Decimal(value) parses the string, including exponent notation, then .normalize() strips any trailing zeros from the internal representation. .as_tuple().exponent contains the number of decimal places the internally stored integer is shifted to the left, so negative numbers specify the number of places to the right of the decimal.
You could try using the Decimal function in python:
abs(Decimal(string_value).as_tuple().exponent)
as explained in
Easy way of finding decimal places

Removing non numeric characters from a string

I have been given the task to remove all non numeric characters including spaces from a either text file or string and then print the new result next to the old characters for example:
Before:
sd67637 8
After:
676378
As i am a beginner i do not know where to start with this task. Please Help
The easiest way is with a regexp
import re
a = 'lkdfhisoe78347834 (())&/&745 '
result = re.sub('[^0-9]','', a)
print result
>>> '78347834745'
Loop over your string, char by char and only include digits:
new_string = ''.join(ch for ch in your_string if ch.isdigit())
Or use a regex on your string (if at some point you wanted to treat non-contiguous groups separately)...
import re
s = 'sd67637 8'
new_string = ''.join(re.findall(r'\d+', s))
# 676378
Then just print them out:
print(old_string, '=', new_string)
There is a builtin for this.
string.translate(s, table[, deletechars])
Delete all characters from s
that are in deletechars (if present), and then translate the
characters using table, which must be a 256-character string giving
the translation for each character value, indexed by its ordinal. If
table is None, then only the character deletion step is performed.
>>> import string
>>> non_numeric_chars = ''.join(set(string.printable) - set(string.digits))
>>> non_numeric_chars = string.printable[10:] # more effective method. (choose one)
'sd67637 8'.translate(None, non_numeric_chars)
'676378'
Or you could do it with no imports (but there is no reason for this):
>>> chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?#[\\]^_`{|}~ \t\n\r\x0b\x0c'
>>> 'sd67637 8'.translate(None, chars)
'676378'
I would not use RegEx for this. It is a lot slower!
Instead let's just use a simple for loop.
TLDR;
This function will get the job done fast...
def filter_non_digits(string: str) -> str:
result = ''
for char in string:
if char in '1234567890':
result += char
return result
The Explanation
Let's create a very basic benchmark to test a few different methods that have been proposed. I will test three methods...
For loop method (my idea).
List Comprehension method from Jon Clements' answer.
RegEx method from Moradnejad's answer.
# filters.py
import re
# For loop method
def filter_non_digits_for(string: str) -> str:
result = ''
for char in string:
if char in '1234567890':
result += char
return result
# Comprehension method
def filter_non_digits_comp(s: str) -> str:
return ''.join(ch for ch in s if ch.isdigit())
# RegEx method
def filter_non_digits_re(string: str) -> str:
return re.sub('[^\d]','', string)
Now that we have an implementation of each way of removing digits, let's benchmark each one.
Here is some very basic and rudimentary benchmark code. However, it will do the trick and give us a good comparison of how each method performs.
# tests.py
import time, platform
from filters import filter_non_digits_re,
filter_non_digits_comp,
filter_non_digits_for
def benchmark_func(func):
start = time.time()
# the "_" in the number just makes it more readable
for i in range(100_000):
func('afes098u98sfe')
end = time.time()
return (end-start)/100_000
def bench_all():
print(f'# System ({platform.system()} {platform.machine()})')
print(f'# Python {platform.python_version()}\n')
tests = [
filter_non_digits_re,
filter_non_digits_comp,
filter_non_digits_for,
]
for t in tests:
duration = benchmark_func(t)
ns = round(duration * 1_000_000_000)
print(f'{t.__name__.ljust(30)} {str(ns).rjust(6)} ns/op')
if __name__ == "__main__":
bench_all()
Here is the output from the benchmark code.
# System (Windows AMD64)
# Python 3.9.8
filter_non_digits_re 2920 ns/op
filter_non_digits_comp 1280 ns/op
filter_non_digits_for 660 ns/op
As you can see the filter_non_digits_for() funciton is more than four times faster than using RegEx, and about twice as fast as the comprehension method. Sometimes simple is best.
You can use string.ascii_letters to identify your non-digits:
from string import *
a = 'sd67637 8'
a = a.replace(' ', '')
for i in ascii_letters:
a = a.replace(i, '')
In case you want to replace a colon, use quotes " instead of colons '.
To extract Integers
Example: sd67637 8 ==> 676378
import re
def extract_int(x):
return re.sub('[^\d]','', x)
To extract a single float/int number (possible decimal separator)
Example: sd7512.sd23 ==> 7512.23
import re
def extract_single_float(x):
return re.sub('[^\d|\.]','', x)
To extract multiple float/float numbers
Example: 123.2 xs12.28 4 ==> [123.2, 12.28, 4]
import re
def extract_floats(x):
return re.findall("\d+\.\d+", x)
Adding into #MoradneJad . You can use the following code to extract integer values, floats and even signed values.
a = re.findall(r"[-+]?\d*\.\d+|\d+", "Over th44e same pe14.1riod of time, p-0.8rices also rose by 82.8p")
And then you can convert the list items to numeric data type effectively using map.
print(list(map(float, a)))
[44.0, 14.1, -0.8, 82.8]
import re
result = re.sub('\D','','sd67637 8')
result >>> '676378'
Convert all numeric strings with or without unit abbreviations. You must indicate that the source string is a decimal comma notation by parameter dec=',' Converting to floats as well as integer is possible. Default conversion is float, but set the parameter toInt=True and the result is an integer. Automatic recognition of unit abbreviations that can be edited in the md dictionary. The key is the unit abbreviation and the value is the multiplier. In this way, the applications of this function are endless. The result is always a number you can calculate with. This all in one function is not the fastest method, but you don't have to worry anymore and it always returns a reliable result.
import re
'''
units: gr=grams, K=thousands, M=millions, B=billions, ms=mili-seconds, mt= metric-tonnes
'''
md = {'gr': 0.001, '%': 0.01, 'K': 1000, 'M': 1000000, 'B': 1000000000, 'ms': 0.001, 'mt': 1000}
seps = {'.': True, ',': False}
kl = list(md.keys())
def to_Float_or_Int(strVal, toInt=None, dec=None):
toInt = False if toInt is None else toInt
dec = '.' if dec is None else dec
def chck_char_in_string(strVal):
rs = None
for el in kl:
if el in strVal:
rs = el
break
return rs
if dec in seps.keys():
dcp = seps[dec]
strVal = strVal.strip()
mpk = chck_char_in_string(strVal)
mp = 1 if mpk is None else md[mpk]
strVal = re.sub(r'[^\de.,-]+', '', strVal)
if dcp:
strVal = strVal.replace(',', '')
else:
strVal = strVal.replace('.', '')
strVal = strVal.replace(',', '.')
dcnm = float(strVal)
dcnm = dcnm * mp
dcnm = int(round(dcnm)) if toInt else dcnm
else:
print('wrong decimal separator')
dcnm = None
return dcnm
Call the function as follows:
pvals = ['-123,456', '-45,145.01 K', '753,159.456', '1,000,000', '985 ms' , '888 745.23', '1.753 e-04']
cvals = ['-123,456', '1,354852M', '+10.000,12 gr', '-87,24%', '10,2K', '985 ms', '(mt) 0,475', ' ,159']
print('decimal point strings')
for val in pvals:
result = to_Float_or_Int(val)
print(result)
print()
print('decimal comma strings')
for val in cvals:
result = to_Float_or_Int(val, dec=',')
print(result)
exit()
The output results:
decimal point strings
-123456.0
-45145010.0
753159.456
1000000.0
0.985
888745.23
0.0001753
decimal comma strings
-123.456
1354852.0
10.00012
-0.8724
10200.0
0.985
475.0
0.159

Categories