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!

9
  • 5
    What is your expected value? Commented Nov 29, 2013 at 12:48
  • Can't reproduce your problem, it returns the correct value for me. Commented Nov 29, 2013 at 12:48
  • You should specify culture in your parse operation. User culture, or invariant culture if its technical data. Commented Nov 29, 2013 at 12:48
  • I think you made a little mistake, try: stringVariable = stringVariable.Replace(".", "").Replace("-", ""); float f = Convert.ToSingle(stringVariable); Commented Nov 29, 2013 at 12:51
  • 1
    @SteveB I don't think culture is necessary to specify, as the value is not decimal(?) Commented Nov 29, 2013 at 13:17

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 see my updated answer, do you get the same behaviour? Regarding Max-value of floats: stackoverflow.com/questions/11857797/…
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).
Well it is a general problem with floating numbers, refer to: stackoverflow.com/questions/1005725/…
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 ;)
|
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]", ""));

1 Comment

The only possibility of non-digits in this case are the chars "." and "-", no need to make a more general... The cast failed, anyway.
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);

1 Comment

Yes, works to cast to a decimal data type... the datatype that I must use is float...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.