473,231 Members | 2,054 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,231 software developers and data experts.

Negative integers and string formating

Problem: I have an application where I need to print integers differently
depending on whether they are positive or negative. To be more specific, I
have to print something that looks like:

"something + 1"
"something - 1"

Note the space between the sign and the number. If I didn't need that
space, I would have no problem. Yes, I do need that space.

I build the format string on the fly, then pass it to another function
which actually fills in the values. Simplified example:

def format(n):
if n 0:
return "something positive + %(argument)d"
# real code has a good half-dozen named keys
elif n < 0:
return "something negative - %(argument)d"
return "blank"

def display(**kwargs):
fs = format(kwargs['argument'])
return fs % kwargs

This works fine for positive and zero values:
'something positive + 1'

but not for negative, due to the extra negative sign:
'something negative - -1'

Are there any string formatting codes that will place a space between the
sign and the number?


Oct 24 '06 #1
3 1624
Steven D'Aprano wrote:
Are there any string formatting codes that will place a space between the
sign and the number?
Not that I know of, why not use the absolute value (after checking if
it is negative),

In [1]: abs(-1)
Out[1]: 1

Oct 24 '06 #2
Steven D'Aprano <st***@REMOVEME.cybersource.com.auwrites:
def display(**kwargs):
fs = format(kwargs['argument'])
return fs % kwargs
def display(**kwargs):
fs = format(kwargs['argument'])
return fs % dict((x, abs(y)) for x,y in kwargs.iteritems())
Oct 24 '06 #3
On Mon, 23 Oct 2006 18:56:21 -0700, Paul Rubin wrote:
Steven D'Aprano <st***@REMOVEME.cybersource.com.auwrites:
>def display(**kwargs):
fs = format(kwargs['argument'])
return fs % kwargs

def display(**kwargs):
fs = format(kwargs['argument'])
return fs % dict((x, abs(y)) for x,y in kwargs.iteritems())
That will do it! Thanks,


Oct 24 '06 #4

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

Similar topics

by: Danny Winslow | last post by:
I get an unexpected result when I add two negative numbers in PHP on SPARC/Solaris 8. The same program works fine on Intel/Linux. I'm using PHP 4.3.1 on both systems. Here is my program,...
by: MLH | last post by:
Am using code below to display memory status. Problem with 4th and 5th ones (dwTotalPageFile and dwAvailPageFile). They show up as NEGATIVE. Why might that be? 'xxxxxxxxxxxxxxxBEGIN...
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
by: Johs | last post by:
I need to make some special action if 'a' and 'b' are both positive or both negative. Is there some inbuilt function to check this? Johs
by: twocansam1 | last post by:
I am writing a routine to check for bad or no input, How can I check for blanks or spaces and negative numers?? negative numbers and spaces just cause the program to lock up. They don't get...
by: Steve67 | last post by:
Need someones help for what I believe is a simple process, but something I am doing is wrong. I am working in Access 2003 program in a simple Jet query. What I am trying to do is subtract two dates...
by: Amyly | last post by:
Hi.. I am trying to write a program itoa() that can convert negative integers to ASCII string. Any help on how to do it would be much appreciated. Thanks, Amyly
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.