I want to get the only that part which is before the point but I am unable to get it with every method. My value is 1.734565456765434E-06. I want to convert it into just 1
Asked
Active
Viewed 97 times
-3
-
1Do you know what number that is supposed to represent? It's not even close to 1. – Jeff Mercado Jun 17 '15 at 04:47
-
I just want a number before point. – Ali Sajid Jun 17 '15 at 04:49
-
1What about 2E+04 - should it be just 2? Some explanation of how you want to convert value would make question more concrete. Adding code you've tried will make question ok for SO. – Alexei Levenkov Jun 17 '15 at 04:49
-
Yes yes exactly if its 2E+04 i want just 2 – Ali Sajid Jun 17 '15 at 04:53
-
http://stackoverflow.com/questions/374316/round-a-double-to-x-significant-figures should give you good starting point. – Alexei Levenkov Jun 17 '15 at 05:02
2 Answers
2
Looks like you're trying to get the most significant digit of a number.
var n = 1.734565456765434E-06;
var exp = Math.Floor(Math.Log10(n)); // -6
var result = Math.Floor(n / Math.Pow(10, exp)); // 1
This can be generalized to this:
var n = 1.734565456765434E-06;
var nDigits = 1; // 1 significant digit
var exp = Math.Floor(Math.Log10(n));
var result = Math.Floor(n / Math.Pow(10, exp + (1 - nDigits)));

Community
- 1
- 1

Jeff Mercado
- 129,526
- 32
- 251
- 272
0
If you are only interested in the first digit of your scientific representation, try this:
var number = 1.734565456765434E-06;
var numberString = number.ToString("E");
var firstDigitString = numberString.Substring(0, 1);
var firstDigit = int.Parse(firstDigitString);

abto
- 1,583
- 1
- 12
- 31