I want to receive the number after the decimal dot in the form of an integer. For example, only 05 from 1.05 or from 2.50 only 50 not 0.50
-
152.50 is just 2.5. I assume you're talking of strings. – Tim Schmelter Oct 23 '12 at 20:15
-
3Are you always returning the first two digits after the decimal place? Is the input a `decimal`, `float`, `string`, ...? – Greg Oct 23 '12 at 20:16
-
3An example of usages goes a long way toward getting quality answers, for next time. – tmesser Oct 23 '12 at 20:16
-
yes only towo digits ,the input is decimal – user1095549 Oct 23 '12 at 20:18
-
Why not use `int y = value.Split('.')[1];`? – SearchForKnowledge Jul 13 '15 at 19:32
-
@SearchForKnowledge, `.` is different in some countries. – Siyavash Hamdi Dec 04 '17 at 10:19
-
@SearchForKnowledge also performing split would take more time than mathematical expressions – Markiian Benovskyi Jan 10 '18 at 13:25
-
4The C# development team should be embarrassed (and take action) that this very common operation has no single Math.function such as Math.DecimalPart(double x) or something. There is way too much "did you try this, did you think about that" for something many of us need to do often. – philologon Jan 15 '18 at 15:44
-
Meta observation: Not getting a "0.50" but getting a *number* of "50" from "2.50", or getting a *number* of "05" is one of the most ill-specified requirements I heard or read today.. not mentioning misleading title of the question. That being said, it's interesting how many of the answers focus on the title and focus on how to calculate the non-integral part of a floating number, *completely ignoring* that the actual question includes *quite specific* formatting requirements :) Congrats to **orad** for answering in full. – quetzalcoatl Feb 02 '18 at 22:00
19 Answers
the best of the best way is:
var floatNumber = 12.5523;
var x = floatNumber - Math.Truncate(floatNumber);
result you can convert however you like

- 3,246
- 4
- 17
- 29
-
15This should be the accepted answer, a Math solution to a Math question, without all that tedious mucking about in string manipulation – johnc Oct 01 '14 at 04:57
-
27I think this solution have a "problem". If the floatNumber was "10.2", the variable x will be something like "0.19999999999999929". Even if this works as expected, the result would be "0.2" and not "2". – Dherik Nov 25 '14 at 15:20
-
4@Dherik Float and Double values always have small errors under subtraction in the last few decimal places. You should not be considering those as significant. If you actually need that kind of precision, you should be using Decimal type. If you can't switch to Decimal (or just don't want to), then you ought to keep track of the expected precision somehow and account for that in comparison operations. I usually use 4 decimal places, but there may be exceptions to that. – philologon Jan 05 '15 at 02:43
-
1From http://msdn.microsoft.com/en-us/library/system.double%28v=vs.110%29.aspx#Precision : "Because some numbers cannot be represented exactly as fractional binary values, floating-point numbers can only approximate real numbers" – philologon Jan 05 '15 at 02:43
-
1Final comment: Using Math.Truncate was the intent of the author's of the Math library for this kind of situation. @Matterai 's answer should be the accepted one. – philologon Jan 05 '15 at 02:49
var decPlaces = (int)(((decimal)number % 1) * 100);
This presumes your number only has two decimal places.

- 28,906
- 14
- 90
- 154

- 7,558
- 2
- 26
- 38
-
1`(318.40d % 1) * 100` outputs `39.9999999999977`, you can use casting to get around the rounding error: `var decPlaces = (int)(((decimal)number % 1) * 100);` – orad Jul 13 '15 at 18:03
-
@orad Good call there - floating point numbers always have these little inaccuracies, so casting it is pretty prudent to ensure consistent behavior. I'll update my answer, though Matterai's is still more technically correct. – tmesser Jul 14 '15 at 05:21
There is a cleaner and ways faster solution than the 'Math.Truncate' approach:
double frac = value % 1;

- 627
- 5
- 2
-
13a note: **for negative values, it is negative**: -12.34 % 1 => -0.33999999999999986 – Alex Poca Apr 04 '19 at 14:09
Solution without rounding problem:
double number = 10.20;
var first2DecimalPlaces = (int)(((decimal)number % 1) * 100);
Console.Write("{0:00}", first2DecimalPlaces);
Outputs: 20
Note if we did not cast to decimal, it would output
19
.
Also:
- for
318.40
outputs:40
(instead of39
) - for
47.612345
outputs:61
(instead of612345
) - for
3.01
outputs:01
(instead of1
)
If you are working with financial numbers, for example if in this case you are trying to get the cents part of a transaction amount, always use the
decimal
data type.
Update:
The following will also work if processing it as a string (building on @SearchForKnowledge's answer).
10.2d.ToString("0.00", CultureInfo.InvariantCulture).Split('.')[1]
You can then use Int32.Parse
to convert it to int.

- 15,272
- 23
- 77
- 113
-
1
-
I used this...10.2d.ToString("0.00", CultureInfo.InvariantCulture).Split('.')[1] – Ziggler May 07 '18 at 21:28
Better Way -
double value = 10.567;
int result = (int)((value - (int)value) * 100);
Console.WriteLine(result);
Output -
56

- 8,281
- 10
- 52
- 88
The simplest variant is possibly with Math.truncate()
double value = 1.761
double decPart = value - Math.truncate(value)

- 99
- 1
- 5
I guess this thread is getting old but I can't believe nobody has mentioned Math.Floor
//will always be .02 cents
(10.02m - System.Math.Floor(10.02m))

- 823
- 10
- 23
-
For the super lazy: `double fract(double x) { return x - System.Math.Floor(x); }` – Felipe Gutierrez Jun 25 '20 at 21:02
var result = number.ToString().Split(System.Globalization.NumberDecimalSeparator)[2]
Returns it as a string (but you can always cast that back to an int), and assumes the number does have a "." somewhere.

- 6,192
- 1
- 30
- 34
int last2digits = num - (int) ((double) (num / 100) * 100);

- 9,657
- 16
- 68
- 84

- 125
- 1
- 6
-
Question is to get first 2 decimal digits. For number `318.401567d` your solution outputs `0.401567` where `40` is expected. – orad Jul 13 '15 at 18:27
public static string FractionPart(this double instance)
{
var result = string.Empty;
var ic = CultureInfo.InvariantCulture;
var splits = instance.ToString(ic).Split(new[] { ic.NumberFormat.NumberDecimalSeparator }, StringSplitOptions.RemoveEmptyEntries);
if (splits.Count() > 1)
{
result = splits[1];
}
return result;
}

- 942
- 9
- 12
string input = "0.55";
var regex1 = new System.Text.RegularExpressions.Regex("(?<=[\\.])[0-9]+");
if (regex1.IsMatch(input))
{
string dp= regex1.Match(input ).Value;
}

- 679
- 5
- 9
-
2It would be awesome if you could add a description on how the regex helps solving the problem – Cleptus Nov 09 '20 at 13:42
var d = 1.5;
var decimalPart = Convert.ToInt32(d.ToString().Split('.')[1]);
it gives you 5
from 1.5
:)

- 4,194
- 12
- 59
- 119
-
-
-
-
-
-
its from the question: `For example, only 05 from 1.05 or from 2.50 only 50 not 0.50` – Inside Man Oct 26 '21 at 08:54
You may remove the dot .
from the double you are trying to get the decimals from using the Remove()
function after converting the double to string so that you could do the operations required on it
Consider having a double _Double
of value of 0.66781
, the following code will only show the numbers after the dot .
which are 66781
double _Double = 0.66781; //Declare a new double with a value of 0.66781
string _Decimals = _Double.ToString().Remove(0, _Double.ToString().IndexOf(".") + 1); //Remove everything starting with index 0 and ending at the index of ([the dot .] + 1)
Another Solution
You may use the class Path
as well which performs operations on string instances in a cross-platform manner
double _Double = 0.66781; //Declare a new double with a value of 0.66781
string Output = Path.GetExtension(D.ToString()).Replace(".",""); //Get (the dot and the content after the last dot available and replace the dot with nothing) as a new string object Output
//Do something

- 5,475
- 3
- 23
- 37
Here's an extension method I wrote for a similar situation. My application would receive numbers in the format of 2.3 or 3.11 where the integer component of the number represented years and the fractional component represented months.
// Sample Usage
int years, months;
double test1 = 2.11;
test1.Split(out years, out months);
// years = 2 and months = 11
public static class DoubleExtensions
{
public static void Split(this double number, out int years, out int months)
{
years = Convert.ToInt32(Math.Truncate(number));
double tempMonths = Math.Round(number - years, 2);
while ((tempMonths - Math.Floor(tempMonths)) > 0 && tempMonths != 0) tempMonths *= 10;
months = Convert.ToInt32(tempMonths);
}
}

- 1,253
- 10
- 12
In my tests this was 3-4 times slower than the Math.Truncate answer, but only one function call. Perhaps someone likes it:
var float_number = 12.345;
var x = Math.IEEERemainder(float_number , 1)

- 199
- 1
- 3
- 15
My answer is based on a suspected use-case behind this question and people coming to this question.
The following example program will display a decimal value in a way that appears like a currency like 34.99 or 1.00 unless it has further precision, in which case it will display the whole precision like, 290.19882 or 128.00001
Please critique. Remember the design is for display.
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(ConvertToString(5.00m));
Console.WriteLine(ConvertToString(5.01m));
Console.WriteLine(ConvertToString(5.99m));
Console.WriteLine(ConvertToString(5.000000191m));
Console.WriteLine(ConvertToString(5.000000191000m));
Console.WriteLine(ConvertToString(51028931298373.000000191000m));
}
public static string ConvertToString(decimal value)
{
decimal whole = Math.Truncate(value);
decimal fractional = value - whole;
return ConvertToString(whole, fractional);
}
public static string ConvertToString(decimal whole, decimal fractional)
{
if (fractional == 0m)
{
return $"{whole:F0}.00";
}
else
{
string fs = fractional.ToString("F28").Substring(2).TrimEnd('0');
return $"{whole:F0}.{fs}";
}
}
}
Results
5.00
5.01
5.99
5.000000191
5.000000191
51028931298373.000000191

- 42,091
- 47
- 181
- 266
Use a regex: Regex.Match("\.(?\d+)")
Someone correct me if I'm wrong here

- 515
- 1
- 5
- 19
-
Not very popular with `Regex.Match` but I've received the following error when trying to get the value of the decimal `ArgumentException was unhandled: parsing "\.(?\d+)" - Unrecognized grouping construct.` – Picrofo Software Oct 23 '12 at 20:45
It is very simple
float moveWater = Mathf.PingPong(theTime * speed, 100) * .015f;
int m = (int)(moveWater);
float decimalPart= moveWater -m ;
Debug.Log(decimalPart);

- 7
- 3
Why not use int y = value.Split('.')[1];
?
The Split()
function splits the value into separate content and the 1
is outputting the 2nd value after the .

- 3,663
- 9
- 49
- 122
-
This will work: `Int32.Parse((12.05123d.ToString("0.00").Split('.')[1]))` – orad Jul 13 '15 at 19:49
-
1Because you are casting it to a string to do string manipulation. I don't think you realize just how slow and wasteful this is. – dmarra Feb 18 '16 at 19:02