473,396 Members | 1,975 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,396 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 5435
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
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...
3
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...
5
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
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)....
2
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
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...
2
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...
15
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.