473,387 Members | 1,700 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,387 software developers and data experts.

How to force positive sign (+) on numeric values

Is it possible to force positive values to have the + sign prefixed on
them?

double f1 = 1024.2;
double f2 = -1024.2;

string.Format( "{0:F}", f1 ); // +1024.2
string.Format( "{0:F}", f2 ); // -1024.2

I was hoping for something either in the string.Format prefix, or
something in the NumberFormatInfo object, but I couldn't seem to find
it.

string.Format( "+{0:F}", f1 );

isn't really an option, because I'm trying to make a grid control
display floating points in this way, and I'm not dynamically
generating the text for each cell.

Thanks,
-ken
Nov 15 '05 #1
3 28068
Hum, could you put your double into a custom class. Then for the ToString()
procedure produce the layout you want?

Class signed_double
{
double m_value;
signed_double(double v) { value = v; }
string ToString() { if (f > 0) then return string.Format{"+{0:F}",
m_value)
else return string.Format{"{0:F}", m_value);
}

I had thought the ToString() function was called by default on the grids.
Not sure about any of this. Just an idea.

"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
Is it possible to force positive values to have the + sign prefixed on
them?

double f1 = 1024.2;
double f2 = -1024.2;

string.Format( "{0:F}", f1 ); // +1024.2
string.Format( "{0:F}", f2 ); // -1024.2

I was hoping for something either in the string.Format prefix, or
something in the NumberFormatInfo object, but I couldn't seem to find
it.

string.Format( "+{0:F}", f1 );

isn't really an option, because I'm trying to make a grid control
display floating points in this way, and I'm not dynamically
generating the text for each cell.

Thanks,
-ken

Nov 15 '05 #2
Ken Durden wrote:
Is it possible to force positive values to have the + sign prefixed on
them?

double f1 = 1024.2;
double f2 = -1024.2;

string.Format( "{0:F}", f1 ); // +1024.2
string.Format( "{0:F}", f2 ); // -1024.2

I was hoping for something either in the string.Format prefix, or
something in the NumberFormatInfo object, but I couldn't seem to find
it.

string.Format( "+{0:F}", f1 );

isn't really an option, because I'm trying to make a grid control
display floating points in this way, and I'm not dynamically
generating the text for each cell.


It took some careful looking at the docs, but I think I found you an
answer.

Using the numeric custom formatting characters, you can specify separate
formats for positive, negative, and zero values in a single format
string by separating them with the semi-colon character:
double f1 = 1024.2;
double f2 = -1024.2;
double f3 = 0.0;

string fmt = "{0:+0.0##;-0.0##;0.0}";

Console.WriteLine( string.Format( fmt, f1 )); // +1024.2
Console.WriteLine( string.Format( fmt, f2 )); // -1024.2
Console.WriteLine( string.Format( fmt, f3 )); // 0.0

You'll possibly want to use the '0' and '#' formatting characters
differently, depending on how many digits you want to force in the
output. If you don't specify a zero format explicitly, it'll use the
positive number format spec (in case that's what you want).

I must say that I prefer the printf() style of forcing a '+' sign in the
output, but this method does have the advantage of being more flexible
(all of the MSDN samples use it to have negative numbers displayed
inside parentheses).

--
mikeb

Nov 15 '05 #3
From the custom numeric format strings section of the docs:

"Different formatting can be applied to a string based on whether the value
is positive, negative, or zero. To produce this behavior, a custom format
string can contain up to three sections separated by semicolons:
a.. One section: The format string applies to all values.
b.. Two sections: The first section applies to positive values and zeros,
and the second section applies to negative values. If the number to be
formatted is negative, but becomes zero after rounding according to the
format in the second section, then the resulting zero is formatted according
to the first section.
c.. Three sections: The first section applies to positive values, the
second section applies to negative values, and the third section applies to
zeros. The second section might be left empty (by having nothing between the
semicolons), in which case the first section applies to all nonzero values.
If the number to be formatted is nonzero, but becomes zero after rounding
according to the format in the first or second section, then the resulting
zero is formatted according to the third section. "
So using two sections, like so will work:
double f1 = 1024.2;
double f2 = -1024.2;
string.Format( "{0:+0.#####;-0.#####}", f1 ); // +1024.2
string.Format( "{0:+0.#####;-0.#####}", f2 ); // -1024.2

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
Is it possible to force positive values to have the + sign prefixed on
them?

double f1 = 1024.2;
double f2 = -1024.2;

string.Format( "{0:F}", f1 ); // +1024.2
string.Format( "{0:F}", f2 ); // -1024.2

I was hoping for something either in the string.Format prefix, or
something in the NumberFormatInfo object, but I couldn't seem to find
it.

string.Format( "+{0:F}", f1 );

isn't really an option, because I'm trying to make a grid control
display floating points in this way, and I'm not dynamically
generating the text for each cell.

Thanks,
-ken

Nov 15 '05 #4

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

Similar topics

2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
5
by: Peter Bailey | last post by:
I have an expression that returns the week number in a row but descending from a date: Week Number: DateDiff("ww",DateAdd("ww",1,forms!FrmBookingCurveInput!TxtStart.value),) It allows me to...
19
by: Harshan | last post by:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why the one less at positive Range ? (compared to the negative side..!) I came to know that it uses 2's compliment...
4
by: bardo | last post by:
Does anyone know if there is a easy way to change a negitive int value into a positive int value. int a = -105 > change this to 105. With other words is there a command to change to neg...
4
by: Kun | last post by:
i have an html/cgi input that takes in values to a mysql database, however, if i stick in $20 instead of 20, it crashes the program because of the extra $ sign. I was wondering if anyone has a...
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: frasmus | last post by:
*Please excuse me for cross-posting. I really can't find a suitable "wireless internet" group to ask this question, except forum.nokia, where I didn't have any luck. I have a web page for...
2
by: tbrogdon | last post by:
I have a form to add new employees to tblEmployee with the following fields: Department (text - linked to tblDept - 3 values in a single field) Shift (numeric - linked to tblShift - 3 values in...
63
by: deepak | last post by:
Hi, Can someone give the standard function which can create positive integer value in C? Thanks, Deepak
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...

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.