473,465 Members | 4,818 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Float to string conversion in C#

Someone else on here had the same issue as I have but the response he
got didnt solve my problem so I hope someone can help me (for some
reason I was unable to reply on the same thread as the original)

I am trying to convert a string to a float. It contains a float value
but when I try:

float myfloat = System.Convert.ToSingle(strfloat);

I get an exception saying something about date time types that I dont
really understand

Help??

TIA
Mish
x

Feb 17 '06 #1
13 19216
"mishj" <mi***@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I get an exception saying something about date time types that I dont
really understand


There error you're getting is easily solved by actually telling us what the
error message is. As soon as you do that, somebody here will solve your
problem for you...
Feb 17 '06 #2
mishj,

Try to call directly float.Parse (it is the same as Single.Parse)

Can you please post the exception message + the string you are trying to
convert?
--
HTH
Stoitcho Goutsev (100)

"mishj" <mi***@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Someone else on here had the same issue as I have but the response he
got didnt solve my problem so I hope someone can help me (for some
reason I was unable to reply on the same thread as the original)

I am trying to convert a string to a float. It contains a float value
but when I try:

float myfloat = System.Convert.ToSingle(strfloat);

I get an exception saying something about date time types that I dont
really understand

Help??

TIA
Mish
x

Feb 17 '06 #3
Hello, mishj!

Can you post the exception text here and, if possible, strfloat value?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 17 '06 #4
mishj <mi***@yahoo.com> wrote:
Someone else on here had the same issue as I have but the response he
got didnt solve my problem so I hope someone can help me (for some
reason I was unable to reply on the same thread as the original)

I am trying to convert a string to a float. It contains a float value
but when I try:

float myfloat = System.Convert.ToSingle(strfloat);

I get an exception saying something about date time types that I dont
really understand

Help??


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 17 '06 #5
Thanks for all replying so quickly!!! ;-) Unfortunately, still no
cigar as yet..

The following is now the code ( I tried the float parse thing)

float fFromLat = float.Parse(this.fromLat.Text);

I am getting the following error from Visual Studio 2005 / .Net 2.0
(not sure if its a bug in VS2005)

"FormatException unhandled by user code: Make sure your method
arguments are in the right format
When converting a string to DateTime, parse the string to take the date
before putting each variable into the DateTime object."

Feb 17 '06 #6
mishj <mi***@yahoo.com> wrote:
Thanks for all replying so quickly!!! ;-) Unfortunately, still no
cigar as yet..

The following is now the code ( I tried the float parse thing)

float fFromLat = float.Parse(this.fromLat.Text);

I am getting the following error from Visual Studio 2005 / .Net 2.0
(not sure if its a bug in VS2005)

"FormatException unhandled by user code: Make sure your method
arguments are in the right format
When converting a string to DateTime, parse the string to take the date
before putting each variable into the DateTime object."


We could do with *complete* code which demonstrates the problem, along
with the text you're trying to parse.

Basically, see http://www.pobox.com/~skeet/csharp/incomplete.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 17 '06 #7
Well I worked it out in the end - because I was returning the result of
a Yahoo! maps api call (which is definitely a float type) into the text
field fromLat before the call to float.Parse, I had assumed that the
value was at that point "in" the text field. It turns out that until
the server side code completes, the textfield doesnt seem to actually
"get" the value passed into it, so at the point I was calling the
float.Parse the text field had nothing in it, even though I do nothing
to change it between then and when the textfield appears on the screen
with the float value clearly inside the text field. Seems rather
bizarre behaviour, but I had never used c# server side code behind an
asp.net page before this so I didnt understand how the text fields were
assigned values.

Sorry - I guess I should have checked beforehand that the float value
really was in this.fromLat.Text!! ;-)

Having said that, the error message completely threw me, and I dont
like the error message given what I was trying to do - it was nothing
to do with DateTime types, so no wonder I was totally confused!
Microsoft should come up with something better than that I feel. Why
do they assume that a float is a DateTime????

Thanks again guys..

Feb 18 '06 #8
This thread not only highlights the importance of not assuming that
something is actually something you think it is, it also highlights the need
for communicating the problem and/or issue correctly and fully.

If the OP had actually read the actual exception message ("Input string was
not in a correct format.") or posted it verbatim, it would have been
painfully obvious that the content of the input string needed looking at.

Again, if the OP had actually read the information presented when the
exception was thrown, it would also have been obvious that a number of
'Troubleshooting tips' were being presented, one of which was a tip which
might be useful if the OP were doing something with a string that was
supposed to represent a DateTime.

As a result, the valuable time and effort of a number of responders was
wasted and I wouldn't blame if they thought twice about responding to posts
from the OP in the future.

Maybe I'm being a bit idealistic here, but this is a plea for posters to
take care when posting, especially:

* copying and pasting where appropriate, instead of attempting to type
something that needs to reported accurately,

* spelling and grammar - mispelled words and/or lack of or misplaced
punctuation are the fastest ways to ensure that something will be
misunderstood or misinterpreted, and

* most importantly, careful reading of documentation - I know how easy
it is misread, misinterpret and misunderstand information.

"mishj" <mi***@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Well I worked it out in the end - because I was returning the result of
a Yahoo! maps api call (which is definitely a float type) into the text
field fromLat before the call to float.Parse, I had assumed that the
value was at that point "in" the text field. It turns out that until
the server side code completes, the textfield doesnt seem to actually
"get" the value passed into it, so at the point I was calling the
float.Parse the text field had nothing in it, even though I do nothing
to change it between then and when the textfield appears on the screen
with the float value clearly inside the text field. Seems rather
bizarre behaviour, but I had never used c# server side code behind an
asp.net page before this so I didnt understand how the text fields were
assigned values.

Sorry - I guess I should have checked beforehand that the float value
really was in this.fromLat.Text!! ;-)

Having said that, the error message completely threw me, and I dont
like the error message given what I was trying to do - it was nothing
to do with DateTime types, so no wonder I was totally confused!
Microsoft should come up with something better than that I feel. Why
do they assume that a float is a DateTime????

Thanks again guys..

Feb 18 '06 #9
I think you are being very idealistic here. I am an experienced IT
professional with 17 years experience in many languages - VB, ASP,
Java, J2EE, JSP, C, HTML and various databases. I have helped a great
many other professionals in my time on many newsgroups and discussion
groups.

There were two reasons for my "failure" here - actually three..
1.This was my first time using the "code behind" model and my first
time using VS 2005. Others had told me that VS 2005 was very buggy.
Given that I had made a call directly before this piece of code to
place the value into the text field, and had checked that the value was
indeed a float, one step before this piece of code, I thought it was
safe to assume that the text field did indeed contain a float. I still
feel it is rather weird that the result of the following code is that
text1.text contains ""
text1.text = 87.87Fand yet it is the last piece of code carried out
before the page is shown to me with the value in the field correctly.
2. The error message I got was very misleading - made me think I had
missed a parameter indicating the format of the data or something - I
could not see what DateTime had to do with my error, and I still cannot
3. I was in a real hurry to find somewhere on the internet that someone
might be able to help me and I posted to many newsgroups and discussion
groups already, with no replies. Until I got all these great replies I
had no idea that this was the place to come - from now on I know where
to come. Sorry for anyone who was inconvenienced.

I am most grateful to all those who tried to help.
If I made any spelling or grammar mistakes please let me know.
And I had carefully read the documentation before asking here. I had
searched MSDN Microsoft newsgroups, devshed, etc. etc And I have a
whole pile of books here which I had used to carefully check that my
code was correct. And it was. I was simply getting erroneous results
because of the weird model that code behind uses to fill text fields.

Feb 21 '06 #10
"mishj" <mi***@yahoo.com> wrote in message
news:11********************@f14g2000cwb.googlegrou ps.com...
I think you are being very idealistic here. I am an experienced IT
professional with 17 years experience in many languages - VB, ASP,
Java, J2EE, JSP, C, HTML and various databases. I have helped a great
many other professionals in my time on many newsgroups and discussion
groups.


If that's true, then you *MUST* know how frustrating it is to read a post
which says "I get an exception saying something about date time types that I
dont really understand"
Feb 21 '06 #11
Well, personally, I would think that anyone who had ever tried
manipulating a single by accidentally passing in an empty string would
recognize my description of the exception without knowing the exact
text of the exception. The exact text of the exception would hardly be
much more useful to such a person. And the reverse is true, anyone
that had never experienced this exception wouldnt be able to help me
any better with or without the full text of the exception vs my
description

Feb 21 '06 #12
mishj wrote:
Well, personally, I would think that anyone who had ever tried
manipulating a single by accidentally passing in an empty string would
recognize my description of the exception without knowing the exact
text of the exception.
Nope. You mentioned DateTime, which isn't actually part of the
exception message itself. I believe the DateTime part was added by
ASP.NET because a FormatException had been thrown, rather than the
FormatException itself mentioning DateTime.
The exact text of the exception would hardly be
much more useful to such a person.


It would - because we'd have seen to start with that it wasn't the
FormatException that mentioned DateTime, it was something which
*wrapped* the FormatException.

If you'd tried to come up with a short but complete program to
demonstrate the problem in the first place, you might not even have
needed to post - and if you *had* needed to post, we'd have been able
to get you to the answer much quicker.

I suggest you read http://catb.org/~esr/faqs/smart-questions.html

Jon

Feb 21 '06 #13
"mishj" <mi***@yahoo.com> wrote in message
news:11********************@g43g2000cwa.googlegrou ps.com...
The exact text of the exception would hardly be much more useful to such a
person.


Utter rubbish - what a stupid comment!
Feb 23 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to...
14
by: Glen Able | last post by:
Should it be possible to create a custom class, 'Float', which would behave as a drop-in replacement for the builtin float type? As mentioned in another thread, I once tried this in rather a...
7
by: Mike | last post by:
I am trying to calculate a 32bit float value from 4 int values. I sucessfully calcluated a 32bit long value: LONG l32BitData = pData; l32BitData <<= 8; l32BitData |= (byte)pData;...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
16
by: homa | last post by:
Hi! How do I do to make t-sql not rounding the result that i returned? For example: 0.9616458*60 = 57,698748 (in any calculator) while following: -------------------------------- declare...
34
by: yaniv.dg | last post by:
hi all, i building an equition with float type now,what function should i use in order to identify is someone enter a char value by mistake?
5
by: Thorsten | last post by:
Hi everyone, I am rather new to C# and have a problem that will probably seem trivial to most of you... but I hope you can still help me nevertheless.. Via the comport, I read the result of a...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
1
by: malick | last post by:
Hello it developers, I'm using PHPExcelReader which I downloaded from sourceforge (http://sourceforge.net/projects/phpexcelreader) To read excel xls files in php. In the file reader.php (...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.