Reputation: 5798
I'm trying to round off the following values, but I don't know what's wrong.
NewValue -=(System.Math.Round(Global.WorkData.ValueOld - System.Math.Round (Global.WorkData.ValueOld * 20, 0) / 20, 2 ));
And this is what I did:
NewValue -=(System.Math.Round(Global.WorkData.ValueOld, 2, MidpointRounding.AwayFromZero - System.Math.Round(Global.WorkData.ValueOld * 20, 2,MidpointRounding.AwayFromZero) / 20, 2 ));
Could you please tell me what's wrong in my code? why I can't compile it? Thanks
Upvotes: 0
Views: 1272
Reputation: 18800
You have missed out a bracket:
NewValue -=(System.Math.Round(Global.WorkData.ValueOld, 2, MidpointRounding.AwayFromZero - System.Math.Round(Global.WorkData.ValueOld * 20, 2,MidpointRounding.AwayFromZero) / 20, 2 ));
NewValue -=(System.Math.Round(Global.WorkData.ValueOld, 2, MidpointRounding.AwayFromZero**)** - System.Math.Round(Global.WorkData.ValueOld * 20, 2,MidpointRounding.AwayFromZero) / 20, 2 );
Hope this helps you out.
I think you're aiming for the following: (but i'm not 100%)
decimal OldValue = Global.WorkData.ValueOld;
decimal OldValueMaths = (OldValue - (OldValue * 20)) / 20;
NewValue -= Math.Round(OldValueMaths, 2, MidpointRounding.AwayFromZero);
Upvotes: 5
Reputation: 1502546
You've got at least one bracketing error, so it's trying to subtract a decimal value from a MidpointRounding
.
However, I still can't work out what you're really trying to do.
I strongly recommend that you split this statement up - split the calls to Round
, and then one using the values. For example:
decimal oldValue = Global.WorkData.ValueOld; // For convenience
decimal roundTo2 = Math.Round(oldValue, 2, MidpointRounding.AwayFromZero);
decimal roundTo20 = Math.Round(oldValue * 20, 2, MidpointRounding.AwayFromZero);
newValue -= roundTo2 - (roundTo20/20);
I still can't work out exactly what you want to do with the results - what's the 2 at the end of the statement meant to be for? What is the first statement in your question meant to be? Maybe you want:
decimal oldValue = Global.WorkData.ValueOld; // For convenience
decimal roundedBy20ths = Math.Round(oldValue * 20, 2,
MidpointRounding.AwayFromZero) / 20;
decimal remainder = Math.Round(oldValue - roundedBy20ths, 2,
MidpointRounding.AwayFromZero);
newValue -= remainder;
? That might be it, but I really don't know...
Shorter statements make the code clearer and let you find this kind of error much more easily.
Upvotes: 4