decimal.parse("value") seems odd

Go To StackoverFlow.com

4

I am working on someone else's code, and see things like:

if ((somevariable) >  decimal.Parse("24,999.99")) ...

and

return int.Parse("0");

I cannot think of any logical reason to do this instead of

if ((somevariable) > 24999.99) ...

or

return 0;

What am I missing?

2012-04-04 22:09
by Outside the Box Developer
did you ask them? Indeed this is completely bizarre - tenfour 2012-04-04 22:12
Maybe it is 24.99999 what is the decimal separator char , or .L.B 2012-04-04 22:13
I cannot ask, as this was a former contractor who did a lot of bizarre things that made no sense to me - Outside the Box Developer 2012-04-04 22:20
"Contractor..." I stopped reading : - Ed S. 2012-04-04 22:29


8

There is a semantic difference between the original code and your proposed change, but you're right in being skeptical.

The conversion from a string is just plain stupid, sorry. There is no need to do that, ever. The difference is that the original code parses the string as a decimal, but your change would use a double. So, it should be:

if (somevariable > 24999.99m) ...
2012-04-04 22:12
by Ed S.
A decimal is also a floating point number - it's just a floating decimal point rather than a floating binary point - Jon Skeet 2012-04-04 22:13
@JonSkeet: Thanks Jon. To be honest I do not know how the decimal type is implemented as C# is more of a hobby language for me, mostly when I need to whip up a Windows UI quickly. Thanks though, I'll fix that and do some reading - Ed S. 2012-04-04 22:14
See http://csharpindepth.com/Articles/General/Decimal.aspx as a starting point :) (C# is only a hobby language for me too... but a pretty big hobby! - Jon Skeet 2012-04-04 22:15
@JonSkeet: Yes apparently : - Ed S. 2012-04-04 22:17
And there are lots of return int.Parse("0"); in various methods as well - Outside the Box Developer 2012-04-04 22:24
@Investor5555: I have no idea why anyone would do such a thing, but it must come from some sort of ignorance. Just make it right - Ed S. 2012-04-04 22:29
To the two dowvoters, an explanation would be nice if I could improve upon this - Ed S. 2012-04-04 22:32
@EdS.: I'm not seeing any downvotes - I'm seeing two downvotes on other answers, but that's all.. - Jon Skeet 2012-04-04 22:33
@JonSkeet: Yeah I hit the cap on this question and after a quick glance thought the discrepancy in points were downvotes. sigh, ok then : - Ed S. 2012-04-04 22:43


7

For one thing, because 24999.99 is a double value rather than a decimal value - you would want to use 24999.99m. But yes, otherwise it would be a much better idea to use the literal. (I wouldn't bother with the parentheses round the variable, either.)

Note that the code performing parsing will even fail if it's run in some cultures, where the decimal separator isn't . and/or the thousands separator isn't ,. I can't think of any good reason for using this.

2012-04-04 22:13
by Jon Skeet
+1 for the comment on different locals as well, I didn't think of that. Darn commas.. - Ed S. 2012-04-04 22:16
I was also going to ask about that. I was about to do some testing with different UI cultures, as I suspect that "1,234" would mean different things - Outside the Box Developer 2012-04-04 22:29
@Investor5555 It sounds like you are viewing the original code as being correct. If you don't want to change the meaning of the original code then you can just leave it as it is. My bet is that the original code is wrong and you should go back to the specifications to work out what the code should be - David Heffernan 2012-04-04 22:32
@Investor5555: Indeed, "1,234" would mean "a bit more than 1" in Germany or France, or "over 1000" in the UK or US - Jon Skeet 2012-04-04 22:32


1

You are not missing anything. That code is bogus. Refactor it the way you describe in the question.

For what it is worth, decimal.Parse("24,999.99") will return 24999.99m, a decimal rather than 24999.99, a double. So the first excerpt should really be

if (somevariable > 24999.99m)

Of course, this assumes that the right hand operand in the comparison really should be a decimal. Given the nature of this code, I would be doubtful of the correctness of everything.

2012-04-04 22:12
by David Heffernan
Well... I'll remove the comment, but if I see an inaccuracy I'm going to comment, best to just post accurate information instead of trying to be first. I didn't downvote : - Ed S. 2012-04-04 22:28
That's true. Comments here are much like comments in code... they quickly become obsolete - Ed S. 2012-04-04 22:43


-1

Was the string within the parse really entered as a string literal, not as a variable?

If so, I would say this is just scary coding, I would only hope they did not put this within a hot spot function. It would then be a sign of a poor coder. I've heard some of those exist.

They may have wanted the benefit of seeing the comma that makes a number more readable. (which says: eeek! be warned about the rest of the code probably...)

2012-04-04 22:17
by Nicholas Petersen
Yes, that was my point. It is not int.Parse(variable) or double.Parse(variable) or .Parse(variable), the code is littered with .Parse("some literal representation") - Outside the Box Developer 2012-04-04 22:21
Ads