4

I have a string in this format: "123.46.789-01". I must cast it to float, and I was doing it this way: float.parse(stringVariable.Replace(".", "").Replace("-", "")) where stringVariable is the string with the value described above.

This cast is generating a wrong cast value, wich is 1.141085E+10.

I've tried converting in many other ways, like Convert.ToSingle, but no success on that. You guys can help me with that? I'm wondering if this kind of number fits on float data type at all...

Thanks in advance!

Johann
  • 67
  • 1
  • 1
  • 7
  • 5
    What is your expected value? – AllenG Nov 29 '13 at 12:48
  • Can't reproduce your problem, it returns the correct value for me. – King King Nov 29 '13 at 12:48
  • You should specify culture in your parse operation. User culture, or invariant culture if its technical data. – Steve B Nov 29 '13 at 12:48
  • I think you made a little mistake, try: `stringVariable = stringVariable.Replace(".", "").Replace("-", ""); float f = Convert.ToSingle(stringVariable);` – online Thomas Nov 29 '13 at 12:51
  • the expected value is "12345678901". I've triedt to specify the culture but makes no difference in this case... I'll try your suggestion, user2754599. EDIT Already tried your suggestion, user2754599, but makes no difference. – Johann Nov 29 '13 at 12:53
  • @Johann I've pretty sure that this does not involve culture at all, the string we parse is just a numeric string, so what's the difference between cultures? – King King Nov 29 '13 at 12:57
  • Try doing it as a double. `double.Parse("12345678901")` works fine on csharpfiddle.com. – Tobberoth Nov 29 '13 at 13:07
  • I get the same value as OP, running it in LINQPad. – cederlof Nov 29 '13 at 13:09
  • 1
    @SteveB I don't think culture is necessary to specify, as the value is not decimal(?) – cederlof Nov 29 '13 at 13:17

3 Answers3

3

There are a lot of problems using floats. I tend to use doubles, which does the same thing(?)

When I run:

var inputString = "123.46.789-01";
var strippedString = inputString.Replace(".", "").Replace("-", "");
float floatValue = float.Parse(strippedString);

I get the value: 1,234679E+09 which is an alternative way of displaying 1234678901.

Confirm by adding this line to the code:

double doubleValue = Convert.ToDouble(floatValue);

and you'll get 1234678901.

cederlof
  • 7,206
  • 4
  • 45
  • 62
  • This code is part of a big system... I can not change the datatype right now... Maybe if I can prove that this size of number (99.999.999.999) can't fit on a float, I get the approval to do it. – Johann Nov 29 '13 at 13:20
  • @Johann see my updated answer, do you get the same behaviour? Regarding Max-value of floats: http://stackoverflow.com/questions/11857797/maximum-value-for-type-float-in-c-sharp – cederlof Nov 29 '13 at 13:21
  • I haven't realized this... But when you save this kind of number, 1,234679E+09, on the database, in a float column, you get the same value? the big picture is that I'm getting another number when this kind of number is stored on the database (SQL SERVER 2008). – Johann Nov 29 '13 at 13:23
  • Well it is a general problem with floating numbers, refer to: http://stackoverflow.com/questions/1005725/save-float-values-in-sql-server – cederlof Nov 29 '13 at 13:25
  • OK. So maybe the float datatype is not a reliable datatype to store this kind of number, huh? EDIT: I've tried to vote up your response, but I don't have the minimum reputation to do it ;) – Johann Nov 29 '13 at 13:31
  • No, I wouldn't use floats, you will get unpredictable results - and that might get you the approval you need! You don't need upvote when you can mark it as the answer ;-) – cederlof Nov 29 '13 at 13:35
1

This works for me and is more general (as I am removing all non-digits):

float result = float.Parse(Regex.Replace(str, "[^0-9]", ""));
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
  • The only possibility of non-digits in this case are the chars "." and "-", no need to make a more general... The cast failed, anyway. – Johann Nov 29 '13 at 12:59
1

Try this! i am getting output 1234678901

string cpf = "123.46.789-01";
decimal result= decimal.Parse(Regex.Replace(cpf, "[^0-9]", ""), System.Globalization.NumberStyles.Any);
Sid
  • 197
  • 12