Using Rounding on a Double

  • Thread starter Thread starter Danny Woolston
  • Start date Start date
D

Danny Woolston

Hi

I have a double that i want to round to a certain number of decimal places
this is the code i use

Return Math.Round(mdNumber, mbDecimalPlaces)

mbDecimalPlaces is the number of decimal places that i want, but it doesn't
use them with two decimal places i still get 0.0 or 9.9

Any ideas?
--


Kind Regards

Danny Woolston
 
You could use the ([number]).ToString([number formatting expression]) like
so:

Dim TestNumber as Double = 1.2345
Dim strNumber as String
strNumber = TestNumber.ToString("#.00000")
'strNumber = "1.23450"
strNumber = TestNumber.ToString("#.00")
'strNumber = "1.23"
strNumber = TestNumber.ToString("00.#")
'strNumber = "01.2345"
 
Hi

Thanks for the reply, but i need the number as a double not a string

--


Kind Regards

Danny Woolston
Software Developer



Jonathan Amend said:
You could use the ([number]).ToString([number formatting expression]) like
so:

Dim TestNumber as Double = 1.2345
Dim strNumber as String
strNumber = TestNumber.ToString("#.00000")
'strNumber = "1.23450"
strNumber = TestNumber.ToString("#.00")
'strNumber = "1.23"
strNumber = TestNumber.ToString("00.#")
'strNumber = "01.2345"
 
If you have .Net V1.1 you can try using the Decimal class. Unlike Double,
it does consider trailing decimal places as significant. It can also store
more data than Double and the precision for decimal number does not drop
off. However, it is bigger and slower than Double (16 bytes vs 8 bytes).

V1.0 did not support this feature on Double. It was changed to comply with
the new ECMA standard.
 
Back
Top