473,573 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5455
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.g ooglegroups.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.c omwrote in message
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
Chad Burggraf wrote:
>cok119 wrote:
>>Hi, all

How can I add '+' or '-' before float value using
float.ToStrin g(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
6660
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual bytes. I've tried using bitwise operators, but they seem to convert the value into an integer first, and i've tried using the toString() method to...
3
7061
by: Robb Gilmore | last post by:
Hello, We have a C#.NET app which is calling a Java webservice. We use the wsdl file exportted from the java webservice to create our web-reference in Visual Studio. We are able to create the parameter classes and call the webservice just fine. Our problem is, within our .Net app, we have some value objects ( like floats, for instance...
5
17423
by: Gustaf Liljegren | last post by:
Values altered when I convert from string to float and back again: using System; class FloatTest { static void Main(string args) { string var1 = "800856.22"; Console.WriteLine(var1);
6
2035
by: ljlevend2 | last post by:
I need to create TypeConverters for the floating type drawing structures (i.e., PointF, SizeF and RectangleF) that behave exactly as their integer couterparts (e.g., System.Drawing.PointConverter). I am able to create TypeConverters with basic functionality by inheriting from System.ComponentModel.ExpandableObjectConverter and overriding...
2
2654
by: Rene | last post by:
The following instruction: float myFloat = float.Parse("30.900665"); Assigns a value to myFloat of "30.9006653", how can this be? Where did the extra 3 came from? Thanks
6
8547
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar circumstances(XML file source) and with exactly the same value it gets it perfectly correct all the time. These are the results I got, XML is...
2
5188
by: Abra | last post by:
Hello, I am using the XmlSerializer clas to serialize/deserialize XML files. I have a XML file containing elements which have attributes of type float. If the value of the float attribute in my application is for example 43.5678, after the serialization I get the value "43.5677999999999". Is it any way to control the formatting of the float...
15
4047
by: soni2926 | last post by:
Hi, I have the following: float.Parse(myproduct.Price.Value.ToString()); myproduct.Price.Value.ToString() returns $24.00 (with the $) Is there anyway to do the above cast, I know the float.Parse(...) doesn't like the $, as i keep getting an invalid cast exception, is there anyway to get this to work? Do i just need to strip out the $...
4
4792
by: Yasin Cepeci | last post by:
I ve get float data from serial port. I ve taken it in the form of hex by modbus protocol. I know it is float but I couldnt convert it there is a few sample data below; B3 33 43 34 = 180.699997 33 33 43 33 = 179.199997 B3 33 43 34= 180.699997 CC CD 43 33=179.800003 But how can I found it. I couldnt resolve it.
0
7679
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7992
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8048
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5281
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3722
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1284
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.