'Python: round(random.uniform) printing wrong float value on output console but in background its correct value
See the following code
import re
import random
s = "~|21|050000|voltage|current|ApparentPower|12345~"
for _ in range(100):
vol_val = round(random.uniform(230, 260), 2)
vol = '{:.2f}'.format(vol_val)
replaced = re.sub('voltage', vol, s)
def cur(match):
return str(round(random.uniform(10,30), 2))
replaced1 = re.sub('current', cur, replaced)
#print(replaced1)
spl_vol = replaced1.split("|")[3]
spl_cur = replaced1.split("|")[4]
print(spl_vol,spl_cur)
mul = float(spl_vol)*float(spl_cur)
dc = format(mul, ".2f")
mull = str(dc)
#print(mull)
txt = replaced1
replacedapp = re.sub("ApparentPower", mull, txt)
print(replacedapp)
In this code iam Generating two random values at each iteration, and using those random values, i am replacing the actual values at each iteration.
At each Iteration i am Replacing Three Values voltage by first voltage random value current by first current random value and I am Replacing ApparentPower by multiplication result of voltage and current.
Here my problem is, voltage and current values displaying wrong values as shown in below (when i printed those values separately, values are correct at background)
~|21|050000|231.86|19.67|4560.69|12345~ --------- printed properly
236.36 26.5 ------------in background current value is correct
~|21|050000|236.36|26263.54|6263.54|12345~ -------- but here current value displayed wrong
234.59 21.59------------ here voltage value at background correct
~|21|050000|234.59|21.59|5064.80|12345~ -------------here also printed correct
236.58 24.14-------------------here voltage value at background correct
~|21|050000|235711.048|24.14|5711.04|12345~ ------------------- but on console displayed wrong
please anyone help me
you can run my code you will get my problem
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|