I have a variable of type double i only want all the amounts to always have two decimal places, how can i go about doing this
When you output it, type it, etc, use .ToString(F2)
Math.Round(doubleVar,2,MidpointRounding.AwayFromZero)
Reference - Adjust double data type to take only the first 2 decimals
when i try this i get the Compiler error(s) encountered processing expression “Math.Round(instalmentSum,2,MidpointRounding.AwayFromZero)”.Invalid L-value expression.:Reference expressions cannot end with Conversion. The provided expression’s type must exactly match the type T of VisualBasicReference or LambdaReference would you know why that is?
Works for me
“dbl” is my variable of type Double
You are all overcomplicating this.
doubleVar.ToString(“F2”) will round it to two decimal points.
Math.round is not over complicating but is the actual way to acheive for double type…unless we want to convert to string …
I think F2 or ,2 is the only difference , although with the round method you have more options that you can work with if needed as well
Cheers
If you want to keep it a double…
CDbl(doubleVar.ToString(“F2”))
It’s just a simple way to round to a certain number of decimal places with standard rounding rules.
You are ready to change double to string and then to double and you say that is not complicated but using math.Round alone is complicated…
Cheers
Complicated for the person, since the OP was unable to get Round to work and would also need to understand what MidpointRounding.AwayFromZero means.
Convert it to a decimal!
Double is decimal. Double is the name of the datatype that stores decimal numbers.
Double is a way of storing decimals, but there is also a decimal datatype, which is better for rounding
That’s not correct. The difference is about mathematical calculations.
But decimal is far slower than double. Unless doing complex mathematical calculations requiring high precision, use double.
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.