Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:

C#
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, 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.
Posted
Comments
Jamie888 19-May-15 23:37pm    
I have done some research on internet and found a method by using Math.Truncate but it seems to return the part which is in front of the decimal not behind of it.
Kenneth Haugland 19-May-15 23:49pm    
https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx
Jamie888 20-May-15 2:21am    
thank you all for your effort and help. I have now able to get the value I want. Thank you.

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

C#
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));
    }
}
 
Share this answer
 
Then you Need a String

myString = myInt.toString("000") ;
 
Share this answer
 
v3
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")
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900