Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 22nd, 2005, 2:37 PM   #1
sojo
Newbie
 
Join Date: Jun 2005
Location: Indianapolis
Posts: 2
Rep Power: 0 sojo is on a distinguished road
Unhappy Need help with rounding numbers[SOLVED]

I know....i know.....sounds stupid...but just look and see if anyone can give me a hand.

I am creating a script that needs to display numbers at the top of a graph. The problem is...the numbers are out to around 9 decimal places...I need them at 1. I have tried the round command, but it doesnt work.

The following is the code...the bold areas being the problem areas.
(and yes...'E'...is a list of numbers.--->E=[.......])

Python will not let me round E...giving me the error message---> typeerror: a float is required. If anyone can help...you have my thanks.

(The {value} input refers to the data= section of code)
-------------------------------------------------------------
def sort_by_value(d):
    """ Returns the keys of dictionary d sorted by their values """
    items=d.items()
    backitems=[ [v[1],v[0]] for v in items]
    backitems.sort()
    return [ backitems[i][1] for i in range(0,len(backitems))]

SBV=sort_by_value

A=SBV(E)

B=sorted(E.values())

#!/usr/bin/python
from pychartdir import *

#The data for the bar chart
data = B

#The labels for the bar chart
labels = ["BMS","ST","FG","CV","LN","CMS","LC","BP","MEC","ABE","HH","IC","MCC","OE","SS","FCV","WR"]

#The colors for the bar chart
colors=[0x000099,0x003300,0x003300,0x003300,0x990000,0x000099,0x990000,0x003300,0x003300,0x003300,0x003300,0x003300,0x990000,0x003300,0x003300,0x000099,0x003300]

#Create a XYChart object of size 260 x 220 pixels. Use golden background color.
#Use a 2 pixel 3D border.
c = XYChart(780, 660, goldColor(), -1, 2)

#Add a title box using 10 point Arial Bold font. Set the background color to
#metallic blue (9999FF) Use a 1 pixel 3D border.
c.addTitle("Educational Adequacy", "arialbd.ttf", 10).setBackground(metalColor(
    0x9999ff), -1, 1)

#Set the plotarea at (40, 40) and of 200 x 150 pixels in size
c.setPlotArea(120, 120, 600, 450)

#Add a multi-color bar chart layer using the given data and colors. Use a 1
#pixel 3D border for the bars.

#Set the labels on the x axis.
c.xAxis().setLabels(labels)

layer = c.addBarLayer(data, c.gradientColor(120, 0, 717, 0, 0xff0000, 0x0000ff))

#Set the bar gap to 10%
layer.setBarGap(0.1)

#Use the format " xxx " as the bar label
layer.setAggregateLabelFormat("{value}")

#Set the bar label font to 10 pts Times Bold Italic/dark red (0x663300)
layer.setAggregateLabelStyle("timesbi.ttf", 10, 0x663300)

#output the chart
c.makeChart("Ecompare.jpg")
--------------------------------------------------------------------

Last edited by sojo; Jun 23rd, 2005 at 10:48 AM. Reason: problem solved
sojo is offline   Reply With Quote
Old Jun 22nd, 2005, 6:27 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,094
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
My Python is broken so I can't do it for you.

But if you want to set something to only one decimal place, you can convert to string, and use [] to only select up to the first decimal place.

Or you could multiply by 10. Convert to integer to kill the rest of the decimals, then convert back to float by dividing by 10.

There are a bunch of other simple methods too...
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is online now   Reply With Quote
Old Jun 23rd, 2005, 9:53 AM   #3
Moldz
Programmer
 
Moldz's Avatar
 
Join Date: Feb 2005
Posts: 54
Rep Power: 4 Moldz is on a distinguished road
Could you explain why round() isn't working? Works fine for me:
E = [1.1111111111, 2.222222222, 3.33333333, 100.5555555, -341.99]
E = [round(i, 1) for i in E]
for num in E:
    print num
It gives me:
1.1
2.2
3.3
100.6
-342.0
Moldz is offline   Reply With Quote
Old Jun 23rd, 2005, 10:51 AM   #4
sojo
Newbie
 
Join Date: Jun 2005
Location: Indianapolis
Posts: 2
Rep Power: 0 sojo is on a distinguished road
Thanks for your help Sane, I worked it out by multiplying by 10
sojo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC