将数字四舍五入到下一个 HIGHEST 10 [英] Round a number to the next HIGHEST 10

查看:39
本文介绍了将数字四舍五入到下一个 HIGHEST 10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个图表,其中 Y 轴的比例根据输入到系统中的数据而变化.可以想象,这个比例可以是 0-10、0-100 之间的任何一个,甚至可以有几千和几百万的上限.

I have a need to create a graph, where the scale of the Y-axis changes depending on the data input into the system. Conceivably this scale could be anywhere from 0-10, 0-100, or even have bottom limit of thousands and an upper limit of millions.

要正确确定此轴的比例,我需要计算点与像素的比率(基于图形高度/范围).现在图表的轴永远不会从最低值开始到最高值,通常的做法是根据值的范围转到下一个最接近的 2、5 或 10(上为上限,下为下限).

To properly determinethe scale of this axis, I need to work out the ratio of Points to Pixels (based on graph height/range). Now a graphs' axis never start at the lowest value and go to the highest, usual practice is to go to the next nearest 2, 5 or 10 (above for upper limit, and below for lower) depending on the range of values.

所以我想知道的是如何从数据集中取最大值,并将其四舍五入到最接近的 10.为澄清起见,输入值将始终为整数.

So what I'd like to know is how to take the max value from the data set, and round it up to the nearest 10. for clarification, the input values will always be integers.

我现在拥有的是这个

        if ((rangeMax < 10) && (rangeMax > 5))
            rangeMax = 10;
        else if (rangeMax < 5)
            rangeMax = 5;

这仅对小于 10 的值有用,并且不允许真正动态所需的灵活性.最终,此图表将在页面加载事件期间自动生成,无需用户输入.

Which is only useful for values less than 10, and doesn't allow the flexibility required to be truly dynamic. Ultimately this graph will be auto-generated during a page load event, with no user input.

我已经阅读了一些内容,人们谈论诸如模数运算符 (%) 之类的东西,但我找不到任何关于它的使用的可靠信息,也没有谈论 Math.Ceiling 和 Math.Round,但是这些转到下一个最近的整数,它不完全存在,并且在处理整数时似乎没有太大帮助.非常感谢任何建议、指示或帮助.

I've read around a bit, and people talk about things like the modulus operator (%), but I can't find any reliable information about it's use, and talk of Math.Ceiling and Math.Round, but these go to the next nearest whole number, which isn't quite there, and don't seem to help much at all when dealing with integers anyway. Any suggestions, pointers or help greatly appreciated.

我确实在这里发现了一个类似的问题 我怎样才能得到 5 或 10 的下一个最大倍数 但我不会 Java,所以我无法理解所说的任何内容.

i did find a similar question asked here How can i get the next highest multiple of 5 or 10 but i don't know java, so i can't understand any of what was said.

干杯

推荐答案

if(rangeMax % 10 !=0)
   rangeMax = (rangeMax - rangeMax % 10) + 10;

你也可以使用 Math.Round()MidpointRounding.AwayFromZero 使用十进制数(否则整数除法会截断分数):

You could also use Math.Round() with MidpointRounding.AwayFromZero using a decimal number (otherwise integer division will truncate fractions):

decimal number = 55M;
decimal nextHighest = Math.Round(number/ 10, MidpointRounding.AwayFromZero) * 10;

这篇关于将数字四舍五入到下一个 HIGHEST 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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