<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Round function to avoid banker's rounding</Title>
      <Shortcut>Roundfunctiontoavoidbanker'srounding</Shortcut>
      <Description>Round function to avoid banker's rounding [VB.NET]</Description>
      <Author>Toby Knott</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=9710a3d4-2021-4e50-b0b3-caa15848405a</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="VB"><![CDATA[' all the framework rounding methods use banker's rounding
' if a number is exactly halfway between two numbers, it will round to the nearest even
' i.e. - 3.5 will round to 4, but 2.5 will round to 2
Function Round(ByVal dVal As Decimal, ByVal lScale As Int32) As Decimal
	Dim lMltply As Int32
	Dim dAdd As Decimal
	' find half of the least significant digit
	dAdd = CType(Math.Pow(0.1, lScale) * 0.5, Decimal)
	If dVal < 0 Then dAdd *= -1
	' initialize multiplier
	lMltply = CType(Math.Pow(10, lScale), Int32)
	' add half of the least significant digit, times the multiplier, truncate,
	' and divide by the multiplier
	Return Decimal.Truncate((dVal + dAdd) * lMltply) / lMltply
End Function]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>