decimal numbers

L

luke.geelen

hello,
i have been working on a python resistor calculator to let my class show what you can do with python.
now i have a script that makes the more speekable value of the resistance (res)

#if len(str(res)) > 9:
# res2 = res / 1000000000
# print "de weerstand is %s,%s giga ohms" % (res2)
#elif len(str(res)) > 6:
# res2 = res / 1000000
# print "de weerstand is %s,%s Mega ohm" % (res2)
#elif len(str(res)) > 3:
# res2 = res / 1000
# print "de weerstand is", res2,"kilo ohm"
#elif len(str(res)) < 4:
# res2 = res
# print "de weerstand is", res2,"ohm"

i commented it because it doesn't work (yet), when i have a resistance of
9.9 Giga ohms it says it is 9 giga ohms. it seems to work with natural number, anyway of using decimals insted so that it says : the resistance is 9.9 Giga Ohms instead of 9 ?
 
L

Luke Geelen

Op zaterdag 15 februari 2014 10:18:36 UTC+1 schreef Luke Geelen:
hello,

i have been working on a python resistor calculator to let my class show what you can do with python.

now i have a script that makes the more speekable value of the resistance (res)



#if len(str(res)) > 9:

# res2 = res / 1000000000

# print "de weerstand is %s,%s giga ohms" % (res2)

#elif len(str(res)) > 6:

# res2 = res / 1000000

# print "de weerstand is %s,%s Mega ohm" % (res2)

#elif len(str(res)) > 3:

# res2 = res / 1000

# print "de weerstand is", res2,"kilo ohm"

#elif len(str(res)) < 4:

# res2 = res

# print "de weerstand is", res2,"ohm"



i commented it because it doesn't work (yet), when i have a resistance of

9.9 Giga ohms it says it is 9 giga ohms. it seems to work with natural number, anyway of using decimals insted so that it says : the resistance is 9.9 Giga Ohms instead of 9 ?

, wait i have put one %s to much in the print function. this is from a other attempt so please excuse me
 
F

Frank Millman

Luke Geelen said:
Op zaterdag 15 februari 2014 10:18:36 UTC+1 schreef Luke Geelen:
hello,

i have been working on a python resistor calculator to let my class show
what you can do with python.

now i have a script that makes the more speekable value of the resistance
(res)
[...]

i commented it because it doesn't work (yet), when i have a resistance of

9.9 Giga ohms it says it is 9 giga ohms. it seems to work with natural
number, anyway of using decimals insted so that it says : the resistance
is 9.9 Giga Ohms instead of 9 ?

You don't say which version of python you are using.

If you are using python2, an integer divided by an integer always returns an
integer -
3

It was changed in python3 to return a float -
3.3333333333333335

You can reproduce the python3 behaviour in python2 by adding a 'future'
directive -
3.3333333333333335

HTH

Frank Millman
 
C

Chris Angelico

If you are using python2, an integer divided by an integer always returns an
integer -

3

It was changed in python3 to return a float -

3.3333333333333335

You can reproduce the python3 behaviour in python2 by adding a 'future'
directive -

3.3333333333333335

Conversely, you can get an integer result by using floor division:
3

ChrisA
 
L

Luke Geelen

Op zaterdag 15 februari 2014 11:04:17 UTC+1 schreef Frank Millman:
Op zaterdag 15 februari 2014 10:18:36 UTC+1 schreef Luke Geelen:


i have been working on a python resistor calculator to let my class show
what you can do with python.
now i have a script that makes the more speekable value of the resistance

[...]

i commented it because it doesn't work (yet), when i have a resistance of
9.9 Giga ohms it says it is 9 giga ohms. it seems to work with natural
number, anyway of using decimals insted so that it says : the resistance
is 9.9 Giga Ohms instead of 9 ?



You don't say which version of python you are using.



If you are using python2, an integer divided by an integer always returns an

integer -



3



It was changed in python3 to return a float -



3.3333333333333335



You can reproduce the python3 behaviour in python2 by adding a 'future'

directive -



3.3333333333333335



HTH



Frank Millman

how (and where) would i add this rule into a script? by import or the calculation?
 
F

Frank Millman

Luke Geelen said:
Op zaterdag 15 februari 2014 11:04:17 UTC+1 schreef Frank Millman:
[...]

You can reproduce the python3 behaviour in python2 by adding a 'future'
directive -
from __future__ import division

3.3333333333333335

how (and where) would i add this rule into a script? by import or the
calculation?

Treat it like any other import - add it as a new line at the top of your
script.

Frank
 
F

Frank Millman

Frank Millman said:
Luke Geelen said:
Op zaterdag 15 februari 2014 11:04:17 UTC+1 schreef Frank Millman:
[...]

You can reproduce the python3 behaviour in python2 by adding a 'future'
directive -

from __future__ import division

10/3

3.3333333333333335

how (and where) would i add this rule into a script? by import or the
calculation?

Treat it like any other import - add it as a new line at the top of your
script.

Actually I did not answer that very accurately. From the documentation -

"""
A future statement must appear near the top of the module. The only lines
that can appear before a future statement are:

the module docstring (if any),
comments,
blank lines, and
other future statements.
"""

Frank
 
L

Laurent Pointal

hello,
i have been working on a python resistor calculator to let my class show
what you can do with python. now i have a script that makes the more
speekable value of the resistance (res)

#if len(str(res)) > 9:
# res2 = res / 1000000000
# print "de weerstand is %s,%s giga ohms" % (res2)
#elif len(str(res)) > 6:
# res2 = res / 1000000
# print "de weerstand is %s,%s Mega ohm" % (res2)
#elif len(str(res)) > 3:
# res2 = res / 1000
# print "de weerstand is", res2,"kilo ohm"
#elif len(str(res)) < 4:
# res2 = res
# print "de weerstand is", res2,"ohm"

i commented it because it doesn't work (yet), when i have a resistance of
9.9 Giga ohms it says it is 9 giga ohms. it seems to work with natural
number, anyway of using decimals insted so that it says : the resistance
is 9.9 Giga Ohms instead of 9 ?

Seem you are using Python2, if res is an integer the division by an integer
values produce an integer result (changed in Python3), so you loose the
decimal part.

Try dividing by floating point numbers, like res2 = res / 1000.


Note: should take a lok at the log10() function from math module.

(with Python3)

In [1]: from math import log10

In [2]: r = 132828378723

In [3]: int(log10(r))
Out[3]: 11

In [4]: r / 10**int(log10(r))
Out[4]: 1.32828378723

A+
Laurent.
 
L

Luke Geelen

If i do set form thing in my script i get
Invalide syntax pointing at the last word of the form rule
 
I

Ian Kelly

hello,
i have been working on a python resistor calculator to let my class show what you can do with python.
now i have a script that makes the more speekable value of the resistance (res)

#if len(str(res)) > 9:
# res2 = res / 1000000000
# print "de weerstand is %s,%s giga ohms" % (res2)
#elif len(str(res)) > 6:
# res2 = res / 1000000
# print "de weerstand is %s,%s Mega ohm" % (res2)
#elif len(str(res)) > 3:
# res2 = res / 1000
# print "de weerstand is", res2,"kilo ohm"
#elif len(str(res)) < 4:
# res2 = res
# print "de weerstand is", res2,"ohm"

i commented it because it doesn't work (yet), when i have a resistance of
9.9 Giga ohms it says it is 9 giga ohms. it seems to work with natural number, anyway of using decimals insted so that it says : the resistance is 9.9 Giga Ohms instead of 9 ?

Others have already explained how to do floating-point rather than
integer division. I'm curious to know why you're basing the if tests
on the length of the number as a string rather than on the magnitude
of the number. Consider for example an input of 0.01. Converted to a
string, that is "0.01" which has a length of 4. So the output would
be "de weerstand is 0.00001 kilo ohm", which is probably not what you
would desire.
 
I

Ian Kelly

If i do set form thing in my script i get
Invalide syntax pointing at the last word of the form rule

Please copy and paste the exact code you ran along with the full text
of the exception into your post. Paraphrasing it like this doesn't
help us help you.
 
L

Luke Geelen

Op zaterdag 15 februari 2014 18:23:20 UTC+1 schreef Ian:
Please copy and paste the exact code you ran along with the full text

of the exception into your post. Paraphrasing it like this doesn't

help us help you.

sorry i made a typo its fixed, thanks a lot
 
L

Luke Geelen

Op zaterdag 15 februari 2014 18:42:51 UTC+1 schreef Luke Geelen:
Op zaterdag 15 februari 2014 18:23:20 UTC+1 schreef Ian:




sorry i made a typo its fixed, thanks a lot

hey, is it possible to remove the .0 if it is a valua without something behind the poit (like 5.0 gets 5 but 9.9 stays 9.9
 
M

Mark Lawrence

Op zaterdag 15 februari 2014 18:42:51 UTC+1 schreef Luke Geelen:

hey, is it possible to remove the .0 if it is a valua without something behind the poit (like 5.0 gets 5 but 9.9 stays 9.9

Thanks for yet more double line spacing and single line paragraphs. Is
it too much to ask for you to follow the instructions that you've
already been given or better yet to use a decent tool?
 
S

Steven D'Aprano

hey, is it possible to remove the .0 if it is a valua without something
behind the poit (like 5.0 gets 5 but 9.9 stays 9.9

Yes, but not easily. First, check the number's fractional part, and if it
is zero, convert it to an int:

# Here, your value is x
if x % 1 == 0:
# Exact whole number.
x = int(x)


But be warned that really big floats are always ints, even if they have a
decimal point in them! For example:

x = 1.1e20

has a decimal point, but its value is:

110000000000000000000.0

which is a whole number. So perhaps a better way to do this is to operate
on the number as a string, not as a numeric value:

s = "%s" % x
if s.endswith(".0"):
s = s[:-2]


If there is anything you don't understand about this code snippet, please
feel free to ask.

By the way, I see that you are using GoogleGroups to post. GoogleGroups
has an extremely obnoxious and annoying habit of throwing in blank lines
between every line of your reply. Could you please read and follow the
instructions here:

https://wiki.python.org/moin/GoogleGroupsPython


otherwise you are likely to annoy people and may find we stop answering
your questions. We are volunteers, and aren't being paid to answer your
questions. If you make it difficult for us, we may just stop.

Thank you in advance.

Any questions, please don't hesitate to ask.
 
S

Steven D'Aprano

The ':g' format specifier will trim off trailing zeroes, e.g.:

'5'

Oh! That's much nicer than my solution. And it even works with %-style
string interpolation too!

py> "%g" % 23.0
'23'
py> "%g" % 23.1
'23.1'


Even though I didn't ask the question, I learned something from the
answer. Thank you.
 
C

Chris Angelico

Even though I didn't ask the question, I learned something from the
answer. Thank you.

Reason #1443694 for mailing lists rather than personal tutoring :) I
love this aspect of them.

ChrisA
 
W

wxjmfauth

Without any warranty.
.... # r: int > 0
.... t = log10(r)
.... if t >= 12.0:
.... prefix = ''
.... prefix2 = ''
.... elif t >= 9.0:
.... prefix = 'giga'
.... prefix2 = 'G'
.... r = r / 1.0e9
.... elif t >= 6.0:
.... prefix = 'mega'
.... prefix2 = 'M'
.... r = r / 1.0e6
.... elif t >= 3.0:
.... prefix = 'kilo-' # Netherlands std?
.... prefix2 = 'k'
.... r = r / 1.0e3
.... else:
.... prefix = ''
.... prefix2 = ''
....
.... # correct for this language (Dutch?) ?
.... # kept as illustrative example
.... if r >= 2:
.... suffix = 's'
.... else:
.... suffix = ''
....
.... # '.13g' to "cover the ranges" while "keeping precision"
.... num = format(r, '.13g')
.... s = 'de weerstand is ' + num + ' ' + prefix + 'ohm' + suffix
.... s = s + ' , R = ' + num + ' ' + prefix2 + 'Ω'
.... return s
....
a = [1, 2, 9, 20, 300, 1000 - 1, 1000, 1000 + 1, 2000, \
.... 1000000 - 1, 1000000, 1000000 + 1, \
.... 1000000000 - 1, 1000000000, 1000000000 + 1, 11123456789].... print(e, '-', z(e))
....
1 - de weerstand is 1 ohm , R = 1 Ω
2 - de weerstand is 2 ohms , R = 2 Ω
9 - de weerstand is 9 ohms , R = 9 Ω
20 - de weerstand is 20 ohms , R = 20 Ω
300 - de weerstand is 300 ohms , R = 300 Ω
999 - de weerstand is 999 ohms , R = 999 Ω
1000 - de weerstand is 1 kilo-ohm , R = 1 kΩ
1001 - de weerstand is 1.001 kilo-ohm , R = 1.001 kΩ
2000 - de weerstand is 2 kilo-ohms , R = 2 kΩ
999999 - de weerstand is 999.999 kilo-ohms , R = 999.999 kΩ
1000000 - de weerstand is 1 megaohm , R = 1 MΩ
1000001 - de weerstand is 1.000001 megaohm , R = 1.000001 MΩ
999999999 - de weerstand is 999.999999 megaohms , R = 999.999999 MΩ
1000000000 - de weerstand is 1 gigaohm , R = 1 GΩ
1000000001 - de weerstand is 1.000000001 gigaohm , R = 1.000000001 GΩ
11123456789 - de weerstand is 11.123456789 gigaohms , R = 11.123456789 GΩ

jmf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top