将小数后面的数字转换为INT /整数 [英] Convert number behind decimal into INT/whole number

查看:149
本文介绍了将小数后面的数字转换为INT /整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我将获得该值。例如,我有值87并通过除以1000来进行一些数学计算。因此它将变为0.087。如何检索值087。我需要在87前面加上0。

以下是我的代码:



Hi, I have the following codes in which I will get the value. For example, I have value "87" and do some maths calculation by dividing it by 1000. Therefore it will become "0.087". How can I retrieve the value "087". I need the "0" in front of the "87".
Below are my codes:

structTIME_OF_DAY_INFO result = new structTIME_OF_DAY_INFO();
int[] TOD_INFO = new int[12];
TOD_INFO[0] = result.itod_hunds;

result.itod_hunds = result.itod_hunds / 1000;





FYI,result.itod_hunds是INTEGER数据类型。将87除以1000之后,它将变为0,因为它在INTERGER数据类型中。由于我的程序有一些标准化限制,我无法将TOD_INFO转换为double。感谢任何帮助。



FYI, the result.itod_hunds is in INTEGER data type. After dividing the 87 by 1000, it will become 0 as it is in INTERGER data type. I cannot convert the "TOD_INFO" into double due to some standardization limitation for my program. Any helps are appreciated.

推荐答案

不是很优雅,但你的整数将被处理。您可以添加其他功能以满足您的业务需求。



Not very elegant, but your integer will be handled. You can add additional functionality to meet your business needs.

using System;

public class Test
{
    public static void Main()
    {
        decimal? decimalValue = Convert.ToDecimal(87);
        string value = ((decimal)(decimalValue / 1000)).ToString("0.000");
        value = value.Substring(1, 4);
        Console.WriteLine(value.Replace(".", string.Empty));
    }
}


然后你需要一个字符串



myString = myInt.toString(000);
Then you Need a String

myString = myInt.toString("000") ;


如果你总是要除以1000,最简单的方法是将它转换为字符串。并使用%而不是/运算符:



If you will always have to divide by 1000, the easiest way to do it is to convert it to string. And use % rather than / operator:

structTIME_OF_DAY_INFO result = new structTIME_OF_DAY_INFO();
int[] TOD_INFO = new int[12];
TOD_INFO[0] = result.itod_hunds;
 
result.itod_hunds = result.itod_hunds % 1000;

//try this if this will work
textBox1.Text = TOD_INFO[0].ToString("000")


这篇关于将小数后面的数字转换为INT /整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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