1 |
On 09/21/2012 08:31:45 AM, Philip Webb wrote: |
2 |
> 120920 Willie WY Wong wrote: |
3 |
> > Unless you want to load the math module every time you start Python, |
4 |
> > it is perhaps better to create an alias in Bash |
5 |
> > using the `-i' option of Python: |
6 |
> > alias python-calc='python -i loadmath.py' |
7 |
> > or if you only need one single command |
8 |
> > alias python-calc='python -i -c "from math import *"' |
9 |
> > which will give you an interactive session with math functions |
10 |
> preloaded. |
11 |
> |
12 |
> Yes thanks: the 2nd is the simplest way to do what I want. |
13 |
> |
14 |
> > 120919 Marc Joliet described how to set a level of precision: |
15 |
> |
16 |
> Thanks too, but that's not what I wanted: it's not for printing, |
17 |
> but simply to limit the display to eg 4 decimal places, not 16 ; |
18 |
> the calculations still wb as accurate, but the output easier to read. |
19 |
> Is that possible with Python ? -- ie a setting in ascript.py |
20 |
> to tell Python to display only the 1st 4 places in all output |
21 |
> without any further input from the user when doing the calculations; |
22 |
> presumably it wb a command s/he could enter when in interactive mode |
23 |
> too. |
24 |
> |
25 |
|
26 |
You could subclass the builtin float class like |
27 |
|
28 |
#!/usr/bin/python3 |
29 |
class myfloat(float) : |
30 |
def __init__(self,value): |
31 |
super().__init__(self,value) |
32 |
def __str__(self): |
33 |
S= super().__str__() |
34 |
return S[:5] |
35 |
|
36 |
X=3.1415926 |
37 |
print(myfloat(X)) |
38 |
|
39 |
|
40 |
Helmut. |