By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

How can I add '+' or '-' before float value using float.ToString() ?

P: n/a
Hi, all

How can I add '+' or '-' before float value using
float.ToString(fotmat-string) ?
What's the format string should used ?

thanks
Jan 2 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
hey

why dont you just do this

"+" + float.ToString();

or
"-" + float.ToString();
Jan 2 '07 #2

P: n/a
cok119 wrote:
Hi, all

How can I add '+' or '-' before float value using
float.ToString(fotmat-string) ?
What's the format string should used ?

thanks
What's wrong with:

String.Format("+{0}", myFloat);

?

Cheers
Chad
Jan 2 '07 #3

P: n/a
Chad Burggraf wrote:
cok119 wrote:
>Hi, all

How can I add '+' or '-' before float value using
float.ToString(fotmat-string) ?
What's the format string should used ?

thanks

What's wrong with:

String.Format("+{0}", myFloat);

?

Cheers
Chad
Hi Chad,

What if the float is negative?

This may be better:
String.Format( "{0}{1}", ((myFloat < 0) ? "-" : "+"), myFloat );

--
Hope this helps,
Tom Spink

Google first, ask later.
Jan 2 '07 #4

P: n/a

cok119 wrote:
How can I add '+' or '-' before float value using
float.ToString(fotmat-string) ?
What's the format string should used ?
Assuming you want the sign of the number to be displayed rather than an
arbitrary '+', then try the following:

a.ToString("+#.###;-#.###;0");

Where 'a' is your variable. This is a format string with sections,
separated by semi-colons. The first section (+#.###) applies to
positive numbers, the second (-#.###) applies to negative numbers and
the last applies to zero.

Jan 2 '07 #5

P: n/a
Hi, Bobbo

It work fine, thank you very much :)

"Bobbo" <ro************@choicequote.co.uk>
??????:11**********************@i12g2000cwa.google groups.com...
>
cok119 wrote:
>How can I add '+' or '-' before float value using
float.ToString(fotmat-string) ?
What's the format string should used ?

Assuming you want the sign of the number to be displayed rather than an
arbitrary '+', then try the following:

a.ToString("+#.###;-#.###;0");

Where 'a' is your variable. This is a format string with sections,
separated by semi-colons. The first section (+#.###) applies to
positive numbers, the second (-#.###) applies to negative numbers and
the last applies to zero.

Jan 2 '07 #6

P: n/a
1 = "+1.0" - as requested
-1 = "--1.0" - not quite

"Tom Spink" <ts****@gmail.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
Chad Burggraf wrote:
>cok119 wrote:
>>Hi, all

How can I add '+' or '-' before float value using
float.ToString(fotmat-string) ?
What's the format string should used ?

thanks

What's wrong with:

String.Format("+{0}", myFloat);

?

Cheers
Chad

Hi Chad,

What if the float is negative?

This may be better:
String.Format( "{0}{1}", ((myFloat < 0) ? "-" : "+"), myFloat );

--
Hope this helps,
Tom Spink

Google first, ask later.

Jan 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.