-2

I want my program to enter a decimal that will output in 4 decimal places by not rounding the inputed number

input: 0.6363636364

output: 0.6363

3
  • 1
    Possible duplicate of Truncate Decimal number not Round Off Commented Apr 17, 2016 at 11:56
  • SALAMAT!!!(thank you) Commented Apr 17, 2016 at 12:15
  • @Naruto Unfortunately, all of the suggestions on that duplicate suffer from possible overflow. The best answer to truncating to a specified number of digits is Tim Lloyd's answer to Truncate Two decimal places without rounding Commented Apr 17, 2016 at 14:18

1 Answer 1

0

For completeness, since the OP requested a VB solution, here's the Decimal extension based on Tim Lloyd's answer to Truncate Two decimal places without rounding:

Module MyExtensions <System.Runtime.CompilerServices.Extension> Public Function TruncateDecimal(d As Decimal, decimals As Integer) As Decimal Select Case True Case decimals < 0 Throw New ArgumentOutOfRangeException("decimals", "Value must be in range 0-28.") Case decimals > 28 Throw New ArgumentOutOfRangeException("decimals", "Value must be in range 0-28.") Case decimals = 0 Return Math.Truncate(d) Case Else Dim IntegerPart As Decimal = Math.Truncate(d) Dim ScalingFactor As Decimal = d - IntegerPart Dim Multiplier As Decimal = Math.Pow(10, decimals) ScalingFactor = Math.Truncate(ScalingFactor * Multiplier) / Multiplier Return IntegerPart + ScalingFactor End Select End Function End Module 

Usage:

Dim Value As Decimal = 0.6363636364 Value = Value.TruncateDecimal(4) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.