2016-04-17 44 views
-2

我想我的程序输入十进制将输出在4位小数通过不四舍五入在输入的号码如何在vb.net窗口应用程序中切割数字?

输入:0.6363636364

输出:0.6363

+1

可能重复的[截断十进制数不舍掉](http://stackoverflow.com/questions/329957/truncate-decimal-number-not-round-off) – Naruto

+0

SALAMAT !!!(谢谢) – hotchongas

+0

@Naruto不幸的是,所有关于'duplicate'的建议都有可能溢出。截断到指定位数的最佳答案是Tim Lloyd的[截断小数点后两位不舍入]的答案(http://stackoverflow.com/a/14629365/3992902) – MrGadget

回答

0

为了完整,因为OP请求VB解决方案,这里的基础上添劳埃德回答十进制扩展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 

用法:

Dim Value As Decimal = 0.6363636364 
Value = Value.TruncateDecimal(4) 
相关问题