PC Review


Reply
Thread Tools Rate Thread

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

 
 
cok119
Guest
Posts: n/a
 
      2nd Jan 2007
Hi, all

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

thanks


 
Reply With Quote
 
 
 
 
=?Utf-8?B?WE9S?=
Guest
Posts: n/a
 
      2nd Jan 2007
hey

why dont you just do this

"+" + float.ToString();

or
"-" + float.ToString();
 
Reply With Quote
 
Chad Burggraf
Guest
Posts: n/a
 
      2nd Jan 2007
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
 
Reply With Quote
 
Tom Spink
Guest
Posts: n/a
 
      2nd Jan 2007
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.
 
Reply With Quote
 
Bobbo
Guest
Posts: n/a
 
      2nd Jan 2007

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.

 
Reply With Quote
 
cok119
Guest
Posts: n/a
 
      2nd Jan 2007
Hi, Bobbo

It work fine, thank you very much

"Bobbo" <(E-Mail Removed)>
??????:(E-Mail Removed)...
>
> 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.
>



 
Reply With Quote
 
gerry
Guest
Posts: n/a
 
      2nd Jan 2007
1 = "+1.0" - as requested
-1 = "--1.0" - not quite

"Tom Spink" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array<float>^ to float* conversion xxx Microsoft VC .NET 5 4th Oct 2006 06:36 PM
why float Martijn Mulder Microsoft C# .NET 16 2nd May 2006 04:43 PM
managed float != unmanaged float Amir Kolsky via .NET 247 Microsoft VC .NET 1 6th Jul 2004 04:40 PM
TypeLoadException: C# interface member float a2[,], implemented as CPP float get_a2 __gc [,] =?Utf-8?B?SGFucyBCZXJlbmRl?= Microsoft C# .NET 0 8th Mar 2004 04:11 PM
HOWTO? float to 4 Bytes, 4 Bytes to float Michael Microsoft C# .NET 3 29th Jul 2003 03:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:07 PM.