10,100,1000,...的倍数 C#
我想要一个整数是 10,100,1000 的倍数等等...
例如 double val = 35 那么我想要 int 40
val = 357 那么我想要 int val = 400
val = 245,567 那么我想要 int val = 300,000
val = 245,567.986 那么我也想要 int = 300,000
C# 中是否有任何内容可以帮助生成这些整数
我能想到的基本逻辑是:提取第一个整数,加 1。计算总位数并添加零 (totalno -1 )。
还有更好的办法吗?
我想将这些值分配给图表轴。我正在尝试根据图表的数据点动态创建轴标签值。
I want an integer to be multiples of 10,100,1000 and so on...
For eg double val = 35 then I want int 40
val = 357 then I want int val = 400
val = 245,567 then I want int val = 300,000
val = 245,567.986 then also I want int = 300,000
Is there anything in C# that can help in generating these integer
Basic logic that I can think is : Extract the first integer , add 1 to it. Count the total number of digits and add zeros (totalno -1 ).
Is there any better way ?
I want to assign these values to the chart axis. I am trying to dynamically create the axis label values based on datapoints of the charts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该做你想要的,其中 x 是输入:
输出:
如果你希望它处理负数(假设你想从 0 舍入):
这给出:
This should do what you want where x is the input:
Output:
If you want it to cope with negative numbers (assuming you want to round away from 0):
Which gives:
这种方法应该适用于 x 的正值和负值:
This approach should work for both positive an negative values of x:
无法为您提供特定于
c#
的答案,但通常您要寻找的是log10
,无论它在c#
中被称为什么。如果你想对数字进行操作。如果这是关于输出,您确实可以对字符串进行操作,跳过/调整第一个数字等。
Can't give you a
c#
-specific answer, but generally what you're looking for islog10
, whatever it's called inc#
. If you want to operate on number.If this is about output, you can, indeed, operate on string, skipping/adjusting first number, etc.
这应该是诀窍:
This should to the trick: