473,765 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1701
On 28 Jul 2005 06:39:32 -0700, ne*****@xit.net <ne*****@xit.ne t> 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('25 0.00')/100

Decimal("2.50")

--
Cheers,
Simon B,
si***@brunningo nline.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.ne t>
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('25 0.00')/100

Decimal("2.50")

--
Cheers,
Simon B,
si***@brunningo nline.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.ne t>
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('25 0.00')/100

Decimal("2.50")

--
Cheers,
Simon B,
si***@brunningo nline.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.ne t> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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.pytho n:

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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 28 '05 #8

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

Similar topics

6
2302
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
2410
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; if(x == y) { printf("x can be represented precisely by a float\n"); }
3
4517
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() via float a = (float ) Queue.ToArray(); float a = Queue.ToArray() as float no luck. Do I have to extract out each element from the Queue and place them into my own allocated array?
20
3147
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 cm ---1 inch = 2.54/100 Meter ---1 yard = 3 feet ---1 feet = 12 inch
6
2298
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 (*f)(float)" But I want to be able to pass a
0
1329
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 * I'm interested if anyone can think of ways to improve this further.
8
6122
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
4036
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 ---------------------------------------------------------------------------------------- float fun(float value) { float f1 =0., f2 = value; float tol = value/1000.; float result,tmp;
22
2773
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 cropped up before, since I rarely use "float"s for anything, and hardly ever use the function for floating-point numbers in the first place; I just was messing around testing it for all cases and noticed a problem.) Anyway, it is declared and I...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.