通过数学Round(上下)进行舍入和舍入

时间:2018-05-14 07:08:48

标签: c# math rounding

我有这样的事情:

double d1 = Math.Round(88.5, 0); // result 88
double d2 = Math.Round(89.5, 0); // result 90

为什么Math.Round()将数字向下舍入,奇数向上舍入?

1 个答案:

答案 0 :(得分:8)

您可以在Math.Round中使用MidpointRounding参数。
当你使用Math.Round时,它的一个重载就是一个带有2个参数的重载,第一个是你的值,第二个是MidpointRounding类型的枚举。

请考虑以下代码:

public class TestBase {         public static WebDriver driver;     public static Properties prop;  public static String configfilepath;
         private static String ID = null;

        public static final String USERNAME = System.getenv("SAUCE_USERNAME");  public static final String ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");  //public static  String user=prop.getProperty("SAUCE_USERNAME");
    //public static  String access_key=prop.getProperty("SAUCE_ACCESS_KEY");
    //public static String url_last=prop.getProperty("Sauce_url_last");     public static final String URL = "http://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub" ;
    //String Sauce_brows=prop.getProperty("Sauce_browser");     protected String sessionId;
    String baseURL=prop.getProperty("baseurl");     long pageload_time = Integer.parseInt(prop.getProperty("Page_load_Time"));  long implicit_time = Integer.parseInt(prop.getProperty("Implicit_Time"));

        public TestBase() throws InvocationTargetException  {
                try         {
            prop=new Properties();
            FileInputStream fi=new FileInputStream("C:\\Users\\Dynamisch Lap\\eclipse-workspace\\PeerTest_Maven\\src\\main\\java\\com\\config\\config.properties");
            //InputStream fi = getClass().getClassLoader().getResourceAsStream("config.properties");
            prop.load(fi);      }
                catch (IOException e)       {           e.printStackTrace();        }
            }       public void initialization() throws MalformedURLException   {        DesiredCapabilities caps =  DesiredCapabilities.chrome();
             caps.setCapability("platform", "Windows 7");
             caps.setCapability("version", "60.0");
             caps.setCapability("recordVideo", "false");
             caps.setCapability("screenResolution", "1280x960");
             caps.setCapability("name", "Peer Test");



            driver=(new RemoteWebDriver(new java.net.URL(URL),caps));

            this.sessionId = (((RemoteWebDriver) driver).getSessionId()).toString();

            driver.get(baseURL);            driver.manage().window().maximize();            driver.manage().timeouts().pageLoadTimeout(pageload_time, TimeUnit.SECONDS);            driver.manage().timeouts().implicitlyWait(implicit_time, TimeUnit.SECONDS);
            }

以下是关于带有MidpointRounding的Math.Round的MSDN文档:
Insertable
以下是关于MidpointRounding的文档:
https://msdn.microsoft.com/en-us/library/ef48waz8(v=vs.110).aspx