Example -> 4.11 I need to get 11 which is the value after decimal
How do i get that in C#?
Example -> 4.11 I need to get 11 which is the value after decimal
How do i get that in C#?
It's old but why dont put the value in an int so it shorts to a number without decimal.
And than subtract the value with the int number like this -->
double myValue = 4.11;
int myInteger = myValue // It's 4
double valueAfterDecimal = myValue - myInteger; // It's 4.11 - 4 = 0.11
int valueAsInt = Convert.ToInt16(valueAfterDecimal * 100) ;
// 0.11 *100 = 11.0 --> Convert to Int = 11
Use this simple method which does preserve leading zeroes of the mantissa:
double number = -1234.056789;
string mantissa = string.Empty;
decimal fraction = (decimal)number % 1;
if (fraction != 0)
{
mantissa = Math.Abs(fraction).ToString().Substring(2);
}
Console.WriteLine(mantissa);
Console.ReadKey();
Result: 056789
If you mean get the digits after the decimal point (if exists) as a raw string, just use string manipulation:
double myNumber = 4.0001115;
string myString = myNumber.ToString();
string decimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
int index = myString.IndexOf(decimalSeparator);
string afterDecimal = (index >= 0 && !myString.EndsWith(decimalSeparator)) ? myString.Substring(index + decimalSeparator.Length) : "";
Otherwise if you mean get it as number by its own right, the other answers here are correct.