473,394 Members | 2,031 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,394 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"
else:
return "blank"

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

This works fine for positive and zero values:
>>display(argument=0)
'blank'
>>display(argument=1)
'something positive + 1'

but not for negative, due to the extra negative sign:
>>display(argument=-1)
'something negative - -1'

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

--
Steven

Oct 24 '06 #1
3 1632
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,

--
Steven

Oct 24 '06 #4

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

Similar topics

0
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,...
2
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...
5
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...
11
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...
39
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,...
19
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
1
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...
6
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...
6
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
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,...
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
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...

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.