我需要调用哪些 VB.NET 舍入方法来获取首选结果? [英] What VB.NET Rounding methods do I need to call to ge the perferred results?

查看:31
本文介绍了我需要调用哪些 VB.NET 舍入方法来获取首选结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 4 个舍入选项:无、标准、向上、向下

I have 4 rounding options: None, Standard, Up, Down

如果他们选择 None 并且数字是 108.7879,我只想显示 108.78 而没有四舍五入.我知道我可能可以将 Math.Ceil 用于 Up,将 Math.Floor 用于 Down,但我不确定对于 None 或 Standard 使用什么.

If they choose None and the number is 108.7879, I just want to display 108.78 with no rounding. I know I probably can use Math.Ceil for Up and Math.Floor for Down, but I am not sure what to use for None or Standard.

如果他们选择标准:

106.78 应四舍五入为 107

106.78 should round up to 107

106.49 应该四舍五入为 106

106.49 should round down to 106

如果他们选择向上:

106.49 应四舍五入为 107

106.49 should round up to 107

106.52 应该四舍五入到 107

106.52 should round up to 107

如果他们选择向下:

106.78 应该四舍五入为 106

106.78 should round down to 106

106.49 应该四舍五入为 106

106.49 should round down to 106

推荐答案

这些就是你想要的:

  • 标准:Math.Round
  • 下:Math.Floor
  • 向上:Math.Ceiling
  • 无:Math.Round(n, 2) 第二个参数指定要四舍五入的位数
  • Standard: Math.Round
  • Down: Math.Floor
  • Up: Math.Ceiling
  • None: Math.Round(n, 2) the second parameter specifies the number of digits to round to

所以这段代码:

imports Microsoft.VisualBasic
imports System

public module MyModule
    Sub Main()
        'If they choose Standard:
        Console.WriteLine(Math.Round(106.78) & " should round up to 107")
        Console.WriteLine(Math.Round(106.49 ) & " should round down to 106")

        'If they choose Up:
        Console.WriteLine(Math.Ceiling(106.49) & "  should round up to 107")
        Console.WriteLine(Math.Ceiling(106.52) & "  should round up to 107")

        'If they choose Down:
        Console.WriteLine(Math.Floor(106.78) & "  should round down to 106")    
        Console.WriteLine(Math.Floor(106.49) & "  should round down to 106")

        'If they choose None:
        Console.WriteLine(Math.Round(106.78455, 2) & "  should round down to 106.78")   
        Console.WriteLine(Math.Round(106.49456, 2) & "  should round down to 106.49")

        Console.ReadKey()
    End Sub
end module

将导致此输出:

107 should round up to 107
106 should round down to 106
107  should round up to 107
107  should round up to 107
106  should round down to 106
106  should round down to 106
106.78  should round down to 106.78
106.49  should round down to 106.49

这篇关于我需要调用哪些 VB.NET 舍入方法来获取首选结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆