0

Hi everyone / Python Gurus

I would like to know how to accomplish the following task, which so far I've been unable to do so.

Here's what I have:

Q1 = 20e-6

Now this is an exponential number that if you print(Q1) as is it will show: 2e-5 which is fine. Mathematically speaking.

However, here's what I want to do with it:

I want Q1 to print only the number 20. And based on the whether this is e-6 then print uC or if this e-9 the print nC.

Here's an example for better understanding:

Q1=20e-6

When I run print(Q1) show: 20uC.

Q2=20e-9

When I run print(Q2) show: 20nC.

Can you please help me figure this out?

jebc88
  • 21
  • 4
  • Only exponent powers 6 and 9 need to be considered here? – Abhishek Agarwal Jun 12 '17 at 06:30
  • Yes, e-6 must be equal to uC and e-9 to nC. No other exponent power should be considered. – jebc88 Jun 12 '17 at 07:00
  • I think python numbers default to the largest possible exponent. As is customary in mathematics, trailing zero's are removed and added in the form of higher exponents. I think you'll have to prepare your numbers first, casting them to string and giving them the desired 'look', in brackets of thousands. – Kraay89 Jun 12 '17 at 08:11

4 Answers4

2

just replace the exponent using str.replace:

q1 = 'XXXXXX'
q1 = q1.replace('e-9', 'nC').replace('e-6', 'uC')
print(q1)
codelessbugging
  • 2,849
  • 1
  • 14
  • 19
  • This is what I get from your code: q1= 20e-6 q1=str(q1) q1 = q1.replace('e-6','uC') print(q1) output: 2e-05 Same issue – jebc88 Jun 12 '17 at 07:04
  • 1
    is your q1 not being declared as a string? – codelessbugging Jun 12 '17 at 07:08
  • python variables are not declared a certain type of data. They adapt data type according to what value is being stored in them. This particular value gets stored as float. Hence why I convert it to STRING afterwards so I could invoke the replace function afterwards. Still no different though. – jebc88 Jun 12 '17 at 07:43
  • pardon my phrasing, i meant if you instantiated `q1` as `q1='20e-6'`, which me and other answerers assumed was the case – codelessbugging Jun 12 '17 at 07:46
  • Sorry for not being clear on that, unfortunately the code should not allow to explicitly declare this variable as a string as you said. Users should only input numeric values such as 20e-6 (without quotes) and have python figure out the rest. Hence the headache. – jebc88 Jun 12 '17 at 08:07
  • In that case we require some rules for resolving which unit to add on to a particular value. For example, 200e-9 can be interpreted as 200nC or 0.2uC. Same for values larger than 10e-6; are they expressed as uC too? – codelessbugging Jun 12 '17 at 08:16
1

I recommend you using si-prefix.

You can install it using pip:

sudo pip install si-prefix

Then you can use something like this:

from si_prefix import si_format

# precision after the point
# char is the unity's char to be used
def get_format(a, char='C', precision=2):
    temp = si_format(a, precision)

    try:
        num, prefix = temp.split()
    except ValueError:
        num, prefix = temp , ''

    if '.' in num:
        aa, bb = num.split('.')
        if int(bb) == 0:
            num = aa

    if prefix:
        return num + ' ' + prefix + char
    else:
        return num


tests = [20e-6, 21.46e05, 33.32e-10, 0.5e03, 0.33e-2, 112.044e-6]
for k in tests:
    print get_format(k)

Output:

20 uC
2.15 MC
3.33 nC
500
3.30 mC
112.04 uC
Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
0

You can try by splitting the string:

'20e-9'.split('e')

gives

['20', '-9']

From there on, you can insert whatever you want in between those values:

('u' if int(a[1]) > 0 else 'n').join(a)

(with a = '20e-9'.split('e'))

Mathias711
  • 6,568
  • 4
  • 41
  • 58
  • This is what I get from your code: q1= 20e-6 q1=str(q1) q1 = q1.split('e') print(q1) output: ['2', '-05'] slightly different issue – jebc88 Jun 12 '17 at 07:07
0

You can not. The behaviour you are looking for is called "monkey patching". And this is not allowed for int and float.

You can refer to this stackoverflow question

The only way I can think of is to create a class that extends float and then implement a __str__ method that shows as per your requirement.

------- More explanation -----

if you type

Q1 = 20e-6

in python shell and then

type(Q1) your will get a

float

So basically your Q1 is considered as float by python type system

when you type print(Q1) the _str__ method of float is called

The process of extending core class is one example of "monkey patch" and that is what I was refereing to.

Now the problem is that you can not "monkey patch" (or extend if you prefer that) core classes in python (which you can in some languages like in Ruby).

[int, float etc are core classes and written in C for your most common python distribution.]

So how do you solve it?

you need to create a new class like this

class Exponent(float): def init(self, value): self.value = value def __str__(self): return "ok"

x = Exponent(10.0)
print(x) ==> "ok"

hope this helps

Tanmay
  • 98
  • 6