-2

Example -> 4.11 I need to get 11 which is the value after decimal

How do i get that in C#?

Nawaz Dhandala
  • 2,046
  • 2
  • 17
  • 23

4 Answers4

0

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
0

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
Gustav
  • 53,498
  • 7
  • 29
  • 55
0

Subtract the value from the math.int of the value

Sparky
  • 14,967
  • 2
  • 31
  • 45
0

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.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208