I have one session var and value of it will be double.How can i do it to be in 2 decimal places rounded up to nearest 5cents.

eg.21.781 -> 21.10

21.322 -> 21.05

45 -> 45.00

0 |

7/1/2008 4:13:12 AM

Math.Round Method (Decimal, Int32, MidpointRounding)

Rounds a decimal value to a specified precision. A parameter specifies how to round the value if it is midway between two other numbers.

Namespace:System## Parameters

dA decimal number to be rounded.

decimalsThe number of significant decimal places (precision) in the return value.

modeSpecification for how to round d if it is midway between two other numbers.

## Return Value

The number nearest d with a precision equal to decimals. If d is halfway between two numbers, one of which is even and the other odd, then mode determines which of the two numbers is returned. If the precision of d is less than decimals, then d is returned unchanged.

Remarks

The decimals parameter specifies the number of significant decimal places in the return value and ranges from 0 to 28. If decimals is zero, an integer is returned.

The behavior of this method follows IEEE Standard 754, section 4. The kind of rounding that is performed if mode is ToEven is sometimes called rounding to nearest, or banker's rounding.

Code Snippet' This example demonstrates the Math.Round() method in conjunction

' with the MidpointRounding enumeration.

Imports System

Class Sample

Public Shared Sub Main()

Dim result As Decimal = 0D

Dim posValue As Decimal = 3.45D

Dim negValue As Decimal = -3.45D

' By default, round a positive and a negative value to the nearest even number.

' The precision of the result is 1 decimal place.

result = Math.Round(posValue, 1)

Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue)

result = Math.Round(negValue, 1)

Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue)

Console.WriteLine()

' Round a positive value to the nearest even number, then to the nearest number

' away from zero. The precision of the result is 1 decimal place.

result = Math.Round(posValue, 1, MidpointRounding.ToEven)

Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", _

result, posValue)

result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero)

Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", _

result, posValue)

Console.WriteLine()

' Round a negative value to the nearest even number, then to the nearest number

' away from zero. The precision of the result is 1 decimal place.

result = Math.Round(negValue, 1, MidpointRounding.ToEven)

Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", _

result, negValue)

result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero)

Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", _

result, negValue)

Console.WriteLine()

End Sub 'Main

End Class 'Sample

'

'This code example produces the following results:

'

' 3.4 = Math.Round( 3.45, 1)

'-3.4 = Math.Round(-3.45, 1)

'

' 3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)

' 3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)

'

'-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)

'-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)

'

Regards,

Punithkumar

0 |

7/1/2008 5:07:25 AM

myaung:I have one session var and value of it will be double.How can i do it to be in 2 decimal places rounded up to nearest 5cents.

eg.21.781 -> 21.10

21.322 -> 21.05

45 -> 45.00

Hi myaung,

Based on your description, you need to format the number in 2 decimal places and rounded it up to nearest 5 cents. But according to your exmaple, I am really confused: I have no idea why 21.781 comes into 21.10 instead of 21.80.

Here I have do a example for you. Have a try on it and tell me is it what you are looking for.

Best Regards,Function doNum(ByVal n As Double) As String doNum = Format(Math.Round(n * 2, 1) / 2, "0.00") End Function

Shengqing Yang

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread : )

0 |

7/2/2008 7:03:39 AM

Hi Yang,

sorry for late reply.

As you pointed,21.781 must be 21.80 .

Your function can really help me.

Thanks alot.

With Best Regards,

myaung

0 |

7/2/2008 3:14:36 PM

The penny (1d) ( Irish : pingin ) coin was the third smallest denomination of the pre-decimal Irish pound , worth 1/240 of a pound or 1/12 of ...

This highly organized system categorizes books on library shelves in an efficient, specific and repeatable order that makes it easy to find any ...

Get Decimal Rounding on the App Store. See screenshots and ratings, and read customer reviews.

Children's librarians represent.

... has created a true monster out of your favorite Sesame Street vampire, with audio of The Count reading every digit of Pi out 10,000 decimal ...

Former VCE Asia-Pacific boss, Paul Harapin, has taken over as CEO of Decimal Software.

The curious case of possibly toxic coins, via Austria and a devastatingly original idea for Canberra.

The BLS reported a jobless rate of 8.3% for both January and February. Adding an extra decimal, the unemployment rates would actually be 8.26% ...

JPMorgan Chase & Co. is being sued by a trader who says he accepted a contract from the investment bank because a typographical error made him ...

It was only back in August when newly-formed Houston game studio Orb Interactive launched a Kickstarter campaign for “.Decimal”, a sci-fi take ...

Resources last updated: 12/15/2015 12:59:38 AM