Re: [Tutor] floats

2011-06-07 Thread Walter Prins
Hello Michael

On 7 June 2011 21:10, Michael bridges  wrote:

> ok, will attempt to clarify.
> i want to out put of two numbers [int or float or anything] to be x.xx not
> x.x.
> i want two numbers after the decimal not one.
>
> Alan's already given you exactly the correct answer.  Have you tried his
suggestions?  What did you not understand or what problems did you run into?

Here's a little example from an interactive Python interpreter session which
shows you how you can play with these concepts to help clarify your
understanding:

Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on
win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> res = 10/float(20)
>>> res
0.5
>>> "%f" % res
'0.50'
>>> "%7.3f" % res
'  0.500'
>>> str = "%6e" % res
>>> str
'5.00e-01'
>>> print str
5.00e-01
>>>

Regards,

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2011-06-07 Thread Michael bridges
ok, will attempt to clarify.
i want to out put of two numbers [int or float or anything] to be x.xx not x.x.
i want two numbers after the decimal not one.


--- On Tue, 6/7/11, Alan Gauld  wrote:

> From: Alan Gauld 
> Subject: Re: [Tutor] floats
> To: tutor@python.org
> Date: Tuesday, June 7, 2011, 1:16 AM
> 
> "Michael bridges" 
> wrote
> 
> > i want to 10 / 1000 and get 0.01 not 0
> > if 1000 is made 1000.00 then 0.01 is printed
> > but that gives 500 / 1000.00 is 0.5 not 0.50
> > 
> > can someone till me how to get a two decimal precision
> every time?
> 
> You are confusing two different things..
> The first case is that of integer versus float division.
> You solve that by either explicitly making one of the
> numbers a float or by converting to float using the
> foloat() operation.
> 
> The second issue is the *representation* of the result.
> The number of decimal places displayed is a matter
> of representation only, the actual value stored will not
> change. Thus 0.5 and 0.50 and 0.5000 are all
> the same value in memory, it is only how they are
> printed that changes and that is controlled by how
> you choose to format the display.
> 
> Typically you use a format string:
> 
> res = 10/float(20)
> "%f" % res
> "%7.3f" % res
> "%5.1f" % res
> "%6e" % res
> "%6.4g" % res
> 
> 
> HTH,
> 
> -- Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2011-06-07 Thread Alan Gauld


"Michael bridges"  wrote


i want to 10 / 1000 and get 0.01 not 0
if 1000 is made 1000.00 then 0.01 is printed
but that gives 500 / 1000.00 is 0.5 not 0.50

can someone till me how to get a two decimal precision every time?


You are confusing two different things..
The first case is that of integer versus float division.
You solve that by either explicitly making one of the
numbers a float or by converting to float using the foloat() 
operation.


The second issue is the *representation* of the result.
The number of decimal places displayed is a matter
of representation only, the actual value stored will not
change. Thus 0.5 and 0.50 and 0.5000 are all
the same value in memory, it is only how they are
printed that changes and that is controlled by how
you choose to format the display.

Typically you use a format string:

res = 10/float(20)
"%f" % res
"%7.3f" % res
"%5.1f" % res
"%6e" % res
"%6.4g" % res


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2011-06-06 Thread Christian Witts

On 2011/06/07 04:43 AM, Michael bridges wrote:

i saw it somewhere, but where?

i want to 10 / 1000 and get 0.01 not 0
if 1000 is made 1000.00 then 0.01 is printed
but that gives 500 / 1000.00 is 0.5 not 0.50

i might be thinking C# not python.

can someone till me how to get a two decimal precision every time?

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



>>> 100/1000
0
>>> from __future__ import division
>>> 100/1000
0.1
>>> 100//1000
0

So you can still get the old behaving floor division using double 
divisors and any normal syntax will be true division. In Python 3.x it's 
already the standard, this is only necessary for Python 2.x


--

Christian Witts

//

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2011-06-06 Thread Modulok
>> Can someone till me how to get a two decimal precision every time?

print "%.2f" % (500/1000.0)

# or...

result = 500 / 1000.0
print "%.2f" % result


Using 'new' style string formatting works too:

print "{0:.2f}".format(500/1000.0)

-Modulok-

On 6/6/11, Michael bridges  wrote:
> i saw it somewhere, but where?
>
> i want to 10 / 1000 and get 0.01 not 0
> if 1000 is made 1000.00 then 0.01 is printed
> but that gives 500 / 1000.00 is 0.5 not 0.50
>
> i might be thinking C# not python.
>
> can someone till me how to get a two decimal precision every time?
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] floats

2011-06-06 Thread Michael bridges
i saw it somewhere, but where?

i want to 10 / 1000 and get 0.01 not 0
if 1000 is made 1000.00 then 0.01 is printed
but that gives 500 / 1000.00 is 0.5 not 0.50

i might be thinking C# not python.

can someone till me how to get a two decimal precision every time?

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2010-12-03 Thread Joel Goldstick
On Fri, Dec 3, 2010 at 1:52 PM, Christopher Spears wrote:

>
> I have a float variable that is very long.
>
> >>> float_a = 1.16667
>
> However, I want to pass the value of float_a to float_b, but I want the
> float to be accurate to two decimal points.
>
> >>> float_a = 1.16667
> >>> print "%.2f" % float_a
> 1.17
>
> I tried the following:
>
> >>> float_b = "%.2f" % float_a
> >>> float_b
> '1.17'
> >>> type(float_b)
> 
>
> This doesn't work because it yields a string.
>
> Any suggestions?
>

This would be good to read:
http://docs.python.org/tutorial/floatingpoint.html

If you have:

float_a = 1.7

and do round(float_a, 2)

you get:
round(1.16667, 2)
1.1699

I don't think that is what you want, but since floating point arithmetic is
done in binary there are rounding errors.

You might want to look at the decimal module

> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2010-12-03 Thread Alan Gauld


"Christopher Spears"  wrote


I have a float variable that is very long.


float_a = 1.16667


Thats not really very long!


However, I want to pass the value of float_a to float_b,
but I want the float to be accurate to two decimal points.


float_a = 1.16667
print "%.2f" % float_a

1.17


Rounding issues will give you all sorts of issues here.
However if you really want to round it up or down to 2 digits
then the round() function is what you want.

But I'd be interested in why you need to lose precision
like that, it's not that common a requirement. Usually controlling
the presentation is sufficient (and preferred).


float_b = "%.2f" % float_a
float_b

'1.17'

type(float_b)



This doesn't work because it yields a string.


It works in that it returns a string representation of what you want.
It doesn't produce a float because *string formatting* can only
produce strings.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2010-12-03 Thread Jerry Hill
On Fri, Dec 3, 2010 at 1:52 PM, Christopher Spears
 wrote:
 float_a = 1.16667
>
> However, I want to pass the value of float_a to float_b, but I want the float 
> to be accurate to two decimal points.

Use the built-in round() function, like this:

>>> a = 1.16667
>>> print a
1.16667
>>> b = round(a, 2)
>>> print b
1.17


-- 
Jerry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2010-12-03 Thread Emile van Sebille

On 12/3/2010 10:52 AM Christopher Spears said...


I have a float variable that is very long.


float_a = 1.16667


However, I want to pass the value of float_a to float_b, but I want the float 
to be accurate to two decimal points.


float_a = 1.16667
print "%.2f" % float_a

1.17

I tried the following:


float_b = "%.2f" % float_a
float_b

'1.17'

type(float_b)



This doesn't work because it yields a string.

Any suggestions?


If you want control over the precision I'd use the decimal module.

>>> x
1.1666
>>> y = decimal.Decimal("%.2f" % x)
>>> y
Decimal('1.17')
>>>


Emile

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2010-12-03 Thread Corey Richardson



On 12/3/2010 1:52 PM, Christopher Spears wrote:

I have a float variable that is very long.


float_a = 1.16667

However, I want to pass the value of float_a to float_b, but I want the float 
to be accurate to two decimal points.


float_a = 1.16667
print "%.2f" % float_a

1.17

I tried the following:


float_b = "%.2f" % float_a
float_b

'1.17'

type(float_b)



This doesn't work because it yields a string.

Any suggestions?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


float_b = float(float_b)
That takes the string and makes it a float.
~Corey Richardson
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] floats

2010-12-03 Thread Nitin Pawar
you have two options
1) either type cast like float_b = float(etcetc)
2) use round method which available inbuilt

On Sat, Dec 4, 2010 at 12:22 AM, Christopher Spears
wrote:

>
> I have a float variable that is very long.
>
> >>> float_a = 1.16667
>
> However, I want to pass the value of float_a to float_b, but I want the
> float to be accurate to two decimal points.
>
> >>> float_a = 1.16667
> >>> print "%.2f" % float_a
> 1.17
>
> I tried the following:
>
> >>> float_b = "%.2f" % float_a
> >>> float_b
> '1.17'
> >>> type(float_b)
> 
>
> This doesn't work because it yields a string.
>
> Any suggestions?
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Nitin Pawar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] floats

2010-12-03 Thread Christopher Spears

I have a float variable that is very long.

>>> float_a = 1.16667

However, I want to pass the value of float_a to float_b, but I want the float 
to be accurate to two decimal points.

>>> float_a = 1.16667
>>> print "%.2f" % float_a
1.17

I tried the following:

>>> float_b = "%.2f" % float_a
>>> float_b
'1.17'
>>> type(float_b)


This doesn't work because it yields a string.

Any suggestions?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Floats and rounding down

2007-01-06 Thread Kent Johnson
Alan Gauld wrote:
> "Toon Pieton" <[EMAIL PROTECTED]> wrote
> 
>> Something like temp = '$' + str(round(x,2)) will make x = $1.537 
>> read as
>> $1.54. That's perfect. However, when x = 1.3, or x = 5.0, it will 
>> display
>> just that: $1.3 or $5. I don't like that - I wan't 1.30 and 5.00, 
>> because
>> that looks much more clean and homogeneous.
>>
>> Is there any - not to complicated - way to do that?
> 
> Yes, use string formatting.
> 
 print "$%0.2f" % 1.3
> $1.30

There is no need to round first, either:
In [1]: print '$%0.2f' % 1.537
$1.54

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Floats and rounding down

2007-01-06 Thread Alan Gauld

"Toon Pieton" <[EMAIL PROTECTED]> wrote

> Something like temp = '$' + str(round(x,2)) will make x = $1.537 
> read as
> $1.54. That's perfect. However, when x = 1.3, or x = 5.0, it will 
> display
> just that: $1.3 or $5. I don't like that - I wan't 1.30 and 5.00, 
> because
> that looks much more clean and homogeneous.
>
> Is there any - not to complicated - way to do that?

Yes, use string formatting.

>>> print "$%0.2f" % 1.3
$1.30

There are a wealth of other variations, worth studying in detail.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Floats and rounding down

2007-01-06 Thread Toon Pieton

Hey all!

A very simple question. I'm writing a simple program which does some
calculations with money. I use floats for that, and yes, I know that floats
arent as perfect as decimals, but its doesnt have to be a perfect program.

I have, however, run into a "visual" problem.

Something like temp = '$' + str(round(x,2)) will make x = $1.537 read as
$1.54. That's perfect. However, when x = 1.3, or x = 5.0, it will display
just that: $1.3 or $5. I don't like that - I wan't 1.30 and 5.00, because
that looks much more clean and homogeneous.

Is there any - not to complicated - way to do that?

Thanks in advance for reading and caring!
Toon Pieton
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor