Say I have...
x = "132.00"
but I'd like to display it to be "132" ...dropping the trailing
zeros...I currently try this
if x.endswith("0") :
x = x[:len(x)-1]
if x.endswith("0") :
x = x[:len(x)-1]
if x.endswith(".") :
x = x[:len(x)-1]
I do it like this because if
x = "132.15" ...i dont want to modify it. But if
x = "132.60" ...I want it to become "132.6"
is there a better way to do this? It seems a bit ugly to me.
T.I.A
(thanks in advance)
Jan 11 '06
17 1177
> How about : if "." in x: x, frac = x.split(".") frac = frac.rstrip("0" ) if frac: x = x + "." + frac
Or simpler still:
if "." in x:
x = x.rstrip("0")
x = x.rstrip(".")
More concise, but slightly less readable IMO:
if "." in x:
x = x.rstrip("0").r strip(".")
--
| Matt Hammond
| R&D Engineer, BBC Research & Development, Tadworth, Surrey, UK.
| http://kamaelia.sf.net/
| http://www.bbc.co.uk/rd/
py wrote: hanz wrote:x = x.rstrip('0.') # removes trailing zeroes and dots
knew there had to be a way, thanks.
But that's not it. :-)
This is a wonderful opportunity for you to learn about unit testing, and
begin the long process of developing good testing habits. Of all the
ideas posted, I believe only Mark Hammond's would correctly pass the
basic obvious test cases, and I don't think anyone (without actually
having checked with tests) should be saying even his is clearly correct.
import unittest
from yourmodule import stripZeros # or whatever you have
class Tests(unittest. TestCase):
def test01(self):
'check zero-stripper'
for input, expected in [
('', ''),
('0', '0'),
('0.0', '0'),
('0.', '0'),
('000.', '000'),
('10', '10'),
('10.0', '10'),
('foo', 'foo'),
('foo.', 'foo'), # ??
('132.15', '132.15'),
('132.60', '132.6'),
('132.00', '132'),
('132.00000', '132'),
('132.000001', '132.000001'),
# add others to taste
]:
self.assertEqua ls(expected, stripZeros(inpu t))
unittest.main()
Change the above test cases to match what you really want if they're not
correct, then run the script and make sure everything passes.
-Peter
Peter Hansen wrote: Of*all*the ideas posted, I believe only Mark Hammond's would correctly pass the basic obvious test cases
Too bad he didn't post at all :-)
Peter
Peter Otten wrote: Peter Hansen wrote:Of all the ideas posted, I believe only Mark Hammond's would correctly pass the basic obvious test cases
Too bad he didn't post at all :-)
D'oh! There was a typo in my message above. Naturally, I meant to
write "M. Hammond" instead of "Mark Hammond". ;-)
Sorry Matt!
-Peter
py wrote: Say I have... x = "132.00"
but I'd like to display it to be "132" ...dropping the trailing zeros...
print '%g' % (float(x),)
might work.
Mel.
Mel Wilson wrote: py wrote: Say I have... x = "132.00"
but I'd like to display it to be "132" ...dropping the trailing zeros...
print '%g' % (float(x),)
might work.
Mel.
The input is a string, %g expects a float, TypeError exception.
Forget about the previous mail, i just saw you were converting the
string to float beforehand, in which case he would more than likely run
into the good ol' float imprecision issue sooner than later.
Not to mention that %g formats to scientific notation (e.g. exponential
format with the exponent always being a multiple of 3), he'd probably
use "%f".
On Wed, 11 Jan 2006 05:58:05 -0800, py wrote: Say I have... x = "132.00"
but I'd like to display it to be "132" ...dropping the trailing zeros...I currently try this
Mucking about with the string is one solution. Here is another:
print int(float(x))
I do it like this because if x = "132.15" ...i dont want to modify it. But if x = "132.60" ...I want it to become "132.6"
Then you want:
x = float("123.60") # full precision floating point value
r = round(x, 1) # rounded to one decimal place
--
Steven. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brian Sabbey |
last post by:
Here is a first draft of a PEP for thunks. Please let me know what you
think. If there is a positive response, I will create a real PEP.
I made a patch that implements thunks as described here. It is available
at:
http://staff.washington.edu/sabbey/py_do
Good background on thunks can be found in ref. .
Simple Thunks
|
by: querypk |
last post by:
What is the fastest way to code this particular block of code below..
I used numeric for this currently and I thought it should be really
fast..
But, for large sets of data (bx and vbox) it takes a long time and I
would like to improve.
vbox = array(m) (size: 1000x1000 approx)
for b in bx:
vbox, b:b ] = 0
vbox, b:b ] = 0
|
by: Ross A. Finlayson |
last post by:
I'm trying to write some C code, but I want to use C++'s std::vector.
Indeed, if the code is compiled as C++, I want the container to
actually be std::vector, in this case of a collection of value types or
std::vector<int>.
So where I would use an int* and reallocate it from time to time in C,
and randomly access it via , then I figure to copy the capacity and
reserve methods, because I just need a growable array.
I get to considering...
|
by: dphizler |
last post by:
This is the basic concept of my code:
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
int main() {
double Gtotal, L, size, distance, theRandom;
double Gi;
int xi, xf, yi, yf, zi, zf;
|
by: dba_222 |
last post by:
Dear experts,
Again, sorry to bother you again with such a seemingly dumb question,
but I'm having some really mysterious results here.
ie.
Create procedure the_test
As
| |
by: Mike |
last post by:
Hello All,
Please, if anyone can point me to the problem, I'd sure appreciate it!
I am very new to VB programming and not a programmer to begin with.
This is part of a Visual Basic 2005 Express Edition program to control a remote basketball
scoreboard display unit.
All I'm trying to do is add 5 byte variables and store the result in an integer
variable. I added a Try/Catch block to take look at things.
This exception occurs only when...
|
by: Peted |
last post by:
Im using c# 2005 express edition
Ive pretty much finished an winforms application and i need to
significantly improve the visual appeal of the interface.
Im totaly stuck on this and cant seem to work out how to start on a
solution.
I have of course used a varienty of componets, mostly radio buttons
with "button" appearence.
|
by: stevemtno |
last post by:
I've got a problem with a web page I'm working on. I have 4 modules - one of them has 2 tabs, two of them have 4 tabs. When the user clicks on the tabs, the content below them changes. However, when the user clicks on the tab (any tab), the browser goes back to the top of the page (I'm assuming it's refreshing, I'm not sure). I'm attaching the JS code below, along with the 2-tab version of the module and its accompanying CSS. Any help will be...
|
by: sdanda |
last post by:
Hi ,
Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity .........
This has to work for more than 8,00,000 of records ..... Can you give some performance tips if you have known
1) For this I am using oci driver ( because I m using oracle 10g) instead of thin driver
2) In that programme I m using prepared statement instead of statement
3) I am...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |