本文介绍了如何在VB.NET中从零开始舍入十进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想把一个像16,714.29到17,000这样的数字进行综合。但是任何时候我使用下面的代码总是会综合到16,714。任何人都可以提供帮助。?
我尝试过:
I want to roundup a number like 16,714.29 to 17,000. But any time i use the code below it always roundup to 16,714. Can any some one help.?
What I have tried:
Try
Dim myval As Double = TextBox17.Text
TextBox18.Text = Math.Round(myval, 0, MidpointRounding.AwayFromZero)
Catch ex As Exception
End Try
推荐答案
Public Function RoundToLeft(d As Double, digits As Integer) As Double
Dim rounding As Double = Math.Pow(10.0, digits)
Return Math.Round(d / rounding, 0, MidpointRounding.AwayFromZero) * rounding
End Function
如果你打电话:
If you call it:
Console.WriteLine(RoundToLeft(d, 0)) -> 16714
Console.WriteLine(RoundToLeft(d, 1)) -> 16710
Console.WriteLine(RoundToLeft(d, 2)) -> 16700
Console.WriteLine(RoundToLeft(d, 3)) -> 17000
Console.WriteLine(RoundToLeft(d, 4)) -> 20000
Public Function Ceiling(ByVal value As Decimal, Optional ByVal nearest As Decimal = 1) As Decimal
Return (Int(value / nearest) - If((value / nearest - Int(value / nearest) > 0D), -1D, 0D)) * nearest
End Function
使用:
Dim newValue = Ceiling(3.14D, 0.5D) ' = 3.5
这篇关于如何在VB.NET中从零开始舍入十进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!