JAVA中Math.round负数的取值

本文详细解释了Math.round()函数的工作原理,特别是在处理正数和负数时的不同行为。通过具体的例子,展示了当输入值为负数且接近-0.5时,如何决定向哪个方向进行舍入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Math.round(x);  

在数轴上取值最接近的整数,中间值(0.5)向右取。

x为正数:小数部分≥0.5时,整数取值向右一个整数,即+1。表现为四舍五入

x为负数:小数部分≤0.5时,相近整数更靠近右侧,所以取值右侧的整数,即原负数的整数部分不变。网上有人称作五舍六入是不准确的!!!

正数四舍五入不再验证

负数验证如下:

Math.round(-8.49999) :-8
Math.round(-8.5)   :-8
Math.round(-8.50001) :-9

Math.round(-8.59999) :-9
Math.round(-8.6)     :-9

数轴上画一下就明白了!

private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isSelecting = true; selectionStart = e.Location; // 记录起点(屏幕坐标) selectionRect = new RectangleF(e.X, e.Y, 0, 0); } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (isSelecting) { // 计算选框范围(保持正数宽高) selectionRect.Width = e.X - selectionRect.X; selectionRect.Height = e.Y - selectionRect.Y; pictureBox1.Invalidate(); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { var logicRect = new RectangleF( viewport.ScreenToLogic( Point.Round(selectionRect.Location), // 将 PointF 四舍五入为 Point pictureBox1.Size ), new SizeF( selectionRect.Width / viewport.Scale, selectionRect.Height / viewport.Scale )); selectedItems = points.Where(item => { var p1 = new PointF(item.fiveX, item.sexY); var p2 = new PointF(item.eightX, item.nineY); return logicRect.Contains(p1) || logicRect.Contains(p2); }).ToList(); pictureBox1.Invalidate(); if (e.Button == MouseButtons.Left && isSelecting) { isSelecting = false; // 将屏幕坐标转换为数据坐标 var inverseMatrix = currentTransform.Clone(); inverseMatrix.Invert(); PointF[] points = { new PointF(selectionRect.Left, selectionRect.Top), new PointF(selectionRect.Right, selectionRect.Bottom) }; inverseMatrix.TransformPoints(points); // 更新数据范围 dataMinX = Math.Min(points[0].X, points[1].X); dataMaxX = Math.Max(points[0].X, points[1].X); dataMinY = Math.Min(points[0].Y, points[1].Y); dataMaxY = Math.Max(points[0].Y, points[1].Y); pictureBox1.Invalidate(); } } private void PictureBox_MouseWheel(object sender, MouseEventArgs e) { // 计算缩放中心点(转换为数据坐标) var inverseMatrix = currentTransform.Clone(); inverseMatrix.Invert(); PointF[] centerPoint = { new PointF(e.X, e.Y) }; inverseMatrix.TransformPoints(centerPoint); // 调整缩放级别 zoomFactor *= e.Delta > 0 ? 1.1f : 0.9f; zoomFactor = Math.Max(0.1f, Math.Min(zoomFactor, 10f)); // 限制缩放范围 // 更新变换 currentTransform = new Matrix(); currentTransform.Translate(_offsetX, _offsetY); currentTransform.Scale(zoomFactor, zoomFactor); currentTransform.Translate(-centerPoint[0].X, -centerPoint[1].Y, MatrixOrder.Append); pictureBox1.Invalidate(); } private float dataMinX; // 根据实际类型替换为double等 private float dataMaxX; private float dataMinY; private float dataMaxY; private void Form1_Load(object sender, EventArgs e) { _offsetX = 50; // 初始化偏移量 _offsetY = 50; // 初始化偏移量 }OverflowException和IndexOutOfRangeException在代码里怎么解决pictureBox1所写的代码里怎么解决
03-25
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值