Float to string conversion in C#

M

mishj

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
 
M

Mark Rae

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

Jon Skeet [C# MVP]

mishj said:
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.
 
S

Stoitcho Goutsev \(100\)

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?
 
M

mishj

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

Jon Skeet [C# MVP]

mishj said:
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
 
M

mishj

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

Stephany Young

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

mishj

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

Mark Rae

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"
 
M

mishj

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
 
J

Jon Skeet [C# MVP]

mishj said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top