473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

easy float question just eludes me

Hullo all !

i have a real easy one here that isn't in my book.
i have a int number that i want to divide by 100 and display to two
decimal places.

like this float(int(Var)/100)
but i need it to display the .00 even if it does not have a .00 value
like this
if Var is 50, i need to display .50 instead of just .5

i know there is an easy way to do this.
thanks

Jul 28 '05 #1
7 1688
On 28 Jul 2005 06:39:32 -0700, ne*****@xit.net <ne*****@xit.net> wrote:
i have a real easy one here that isn't in my book.
i have a int number that i want to divide by 100 and display to two
decimal places.

like this float(int(Var)/100)
but i need it to display the .00 even if it does not have a .00 value
like this
if Var is 50, i need to display .50 instead of just .5

import decimal as dec
dec.Decimal('250.00')/100

Decimal("2.50")

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 28 '05 #2
way cool, outta work out just fine

On 07/28/2005 08:48:27 AM, Simon Brunning wrote:
On 28 Jul 2005 06:39:32 -0700, ne*****@xit.net <ne*****@xit.net>
wrote:
i have a real easy one here that isn't in my book.
i have a int number that i want to divide by 100 and display to two
decimal places.

like this float(int(Var)/100)
but i need it to display the .00 even if it does not have a .00

value
like this
if Var is 50, i need to display .50 instead of just .5

import decimal as dec
dec.Decimal('250.00')/100

Decimal("2.50")

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/


Jul 28 '05 #3
ne*****@xit.net writes:
Hullo all !

i have a real easy one here that isn't in my book.
i have a int number that i want to divide by 100 and display to two
decimal places.

like this float(int(Var)/100)
but i need it to display the .00 even if it does not have a .00 value
like this
if Var is 50, i need to display .50 instead of just .5

i know there is an easy way to do this.

print "%.2f" % (50./100)

0.50

Jul 28 '05 #4
oops, i dont seem to have a module decimal
-shawn

On 07/28/2005 08:48:27 AM, Simon Brunning wrote:
On 28 Jul 2005 06:39:32 -0700, ne*****@xit.net <ne*****@xit.net>
wrote:
i have a real easy one here that isn't in my book.
i have a int number that i want to divide by 100 and display to two
decimal places.

like this float(int(Var)/100)
but i need it to display the .00 even if it does not have a .00

value
like this
if Var is 50, i need to display .50 instead of just .5

import decimal as dec
dec.Decimal('250.00')/100

Decimal("2.50")

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/


Jul 28 '05 #5
ne*****@xit.net wrote:
Hullo all !

i have a real easy one here that isn't in my book.
i have a int number that i want to divide by 100 and display to two
decimal places.

like this float(int(Var)/100)
but i need it to display the .00 even if it does not have a .00 value
like this
if Var is 50, i need to display .50 instead of just .5

i know there is an easy way to do this.
thanks


Play with this:
"%.2f"%0.5 '0.50' a = 50.0
("%.2f" % (a/100))[-3:] '.50'
I presume that a stays in the range 0 <= a < 100.
If not you will have to handle the integral digits as well with
something like
"%4.2f" % 3.1 '3.10' "%6.2f" % 3.1 ' 3.10'


regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Jul 28 '05 #6

<ne*****@xit.net> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hullo all !

i have a real easy one here that isn't in my book.
i have a int number that i want to divide by 100 and display to two
decimal places.

like this float(int(Var)/100)


At present, int/int truncates, so this won't do what you want. Use
int/100.0 to get float you want. For output, use % formating operator with
f specification.
'%7.2f' % (50/100.0)

' 0.50'

Terry J. Reedy

Jul 28 '05 #7
On 28 Jul 2005 06:39:32 -0700, ne*****@xit.net declaimed the following
in comp.lang.python:

like this float(int(Var)/100)
First problem -- you are (or were, the operators have been
changing in Python) doing an integer/integer divide which (used to)
returns an integer, and integers don't have fractional parts; and /then/
you are telling it to convert this integer result to a float.

You need to convert the division to floats before performing it:
i = 50
i/100 # integer/integer 0 float(i)/100 # convert to float/integer 0.5 i/100.0 # integer/float 0.5 but i need it to display the .00 even if it does not have a .00 value
like this
if Var is 50, i need to display .50 instead of just .5
Display is formatting, separate from computational usage...
"%7.2f" % (i/100.0) ' 0.50' "%.2f" % (float(i)/100)

'0.50'

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 28 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave win | last post by:
Hi all: I'm confused with the expression "(float *())". Book says that this is a cast. But, I have no idea of this expr. why could this expr ignore the variable??? Thanx!!!
9
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x;...
3
by: troy anderson | last post by:
I have a System.Collections.Queue instance filled with floats and I would like to convert it into a float because a third party plotting package requires it. I have tried casting Queue.ToArray()...
20
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51...
6
by: Martin Bootsma | last post by:
I have a C question, which looks very easy, but no one here seems to know an easy answer. I have a function "powell" (from Numerical Recipes) which takes an argument of the type "double...
0
by: Ron Adam | last post by:
Sometimes it's good to check things while working on them. This seems like a good way to do that. You could probably put these in a module and just import them. from validators import * ...
8
by: vjnr83 | last post by:
Hi, I have a doubt: what is the difference between float **p and float *p? Thanks in advance, Vijay
12
by: kostas | last post by:
Hi I was asked to propose an interview question for C/C++ programmers (CAD/CAE software) I came up with the following...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.