Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I write the program and need to work with last digit of output and display this digit in binary but I need to extract that last digit and then working with it . I do not know how I can do that.

What I have tried:

i try to write the code in c# for solve this problem
Posted
Updated 6-Oct-17 9:56am

That's not going to be easy - a double in C# only has 15~16 digit precision: double (C# Reference) | Microsoft Docs[^] so the normal "multiply and remove teh integer part is not goign to work. In fact, it quite likely that the "15th digit after the float point" is not stored at all - bear in mind that floating point numbers are representations of binary stored data, and are not necesssarily accurate.
One way to do it is a "brute force and ignorance" approach: convert it to a string, and get the digits from that.

But a better solution is to look at exactly what you get the number from, and see how it's generated - my guess would be that the input data isn't 15 digit accurate, so it's unlikely that the value you are playing with is really accurate at that precision.
 
Share this answer
 
I find the best solution by convert the number to string then by using the sequence function extract the last character then convert it to integer. if a=0.1245378 then the integer become equal 8.

C#
string s = Convert.ToString(a);
string l = s.Substring(s.Length -1);
int integer = Convert.ToInt16(l);
 
Share this answer
 

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