471,594 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

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

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
6 5325
hey

why dont you just do this

"+" + float.ToString();

or
"-" + float.ToString();
Jan 2 '07 #2
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
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

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
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
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.

Similar topics

3 posts views Thread by Robb Gilmore | last post: by
5 posts views Thread by Gustaf Liljegren | last post: by
2 posts views Thread by Rene | last post: by
15 posts views Thread by soni2926 | last post: by
4 posts views Thread by Yasin Cepeci | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.