10,100,1000,...的倍数 C#

发布于 2024-08-05 11:09:21 字数 344 浏览 3 评论 0原文

我想要一个整数是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

留蓝 2024-08-12 11:09:21

这应该做你想要的,其中 x 是输入:

        double scale = Math.Pow(10, (int)Math.Log10(x));
        int val = (int)(Math.Ceiling(x / scale) * scale);

输出:

 35          40
 357         400
 245567      300000
 245567.986  300000

如果你希望它处理负数(假设你想从 0 舍入):

        double scale = (x == 0 ? 1.0 : Math.Pow(10, (int)Math.Log10(Math.Abs(x))));
        int val = (int)(Math.Ceiling(Math.Abs(x) / scale) * scale)*  Math.Sign(x);

这给出:

-35         -40
 0           0
 35          40
 357         400
 245567      300000
 245567.986  300000

This should do what you want where x is the input:

        double scale = Math.Pow(10, (int)Math.Log10(x));
        int val = (int)(Math.Ceiling(x / scale) * scale);

Output:

 35          40
 357         400
 245567      300000
 245567.986  300000

If you want it to cope with negative numbers (assuming you want to round away from 0):

        double scale = (x == 0 ? 1.0 : Math.Pow(10, (int)Math.Log10(Math.Abs(x))));
        int val = (int)(Math.Ceiling(Math.Abs(x) / scale) * scale)*  Math.Sign(x);

Which gives:

-35         -40
 0           0
 35          40
 357         400
 245567      300000
 245567.986  300000
我纯我任性 2024-08-12 11:09:21

这种方法应该适用于 x 的正值和负值:

int log = (x == 0) ? 1 : (int)(Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(x)))));
int result = (int)(((x < 0) ? Math.Floor(x / log) : Math.Ceiling(x / log)) * log);

This approach should work for both positive an negative values of x:

int log = (x == 0) ? 1 : (int)(Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(x)))));
int result = (int)(((x < 0) ? Math.Floor(x / log) : Math.Ceiling(x / log)) * log);
满意归宿 2024-08-12 11:09:21

无法为您提供特定于 c# 的答案,但通常您要寻找的是 log10,无论它在 c# 中被称为什么。如果你想对数字进行操作。

如果这是关于输出,您确实可以对字符串进行操作,跳过/调整第一个数字等。

Can't give you a c#-specific answer, but generally what you're looking for is log10, whatever it's called in c#. If you want to operate on number.

If this is about output, you can, indeed, operate on string, skipping/adjusting first number, etc.

掐死时间 2024-08-12 11:09:21

这应该是诀窍:

// val is the value
var log = Math.Floor(Math.Log10(val));
var multiplier = Math.Pow(10, log);

var result = Math.Ceiling(val/multiplier)*multiplier;

This should to the trick:

// val is the value
var log = Math.Floor(Math.Log10(val));
var multiplier = Math.Pow(10, log);

var result = Math.Ceiling(val/multiplier)*multiplier;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文