Get the decimal from double

I want to get the number after the decimal point as an integer. For example, only 05 from 1.05 or from 2.50 total 50 not 0.50

+42
c #
Oct 23 '12 at 20:13
source share
14 answers

Updated Answer

Here I give three approaches for the same.

[1] Mathematical solution using Math.Truncate

var float_number = 12.345; var result = float_number - Math.Truncate(float_number); 

// input: 1.05
// output: "0.050000000000000044"

// input: 10.2
// output: 0.1999999999999992929

If this is not the result you expect, then you need to change the result to the form you want (but you can do some string manipulation again.)

[2] using the factor [which is 10 to the power N (for example, 10² or 10³), where N is the number of decimal places]

  // multiplier is " 10 to the power of 'N'" where 'N' is the number // of decimal places int multiplier = 1000; double double_value = 12.345; int double_result = (int)((double_value - (int)double_value) * multiplier); 

// output 345

If the number of decimal places is not fixed, then this approach can create problems.

[3] using "Regular Expressions (REGEX)"

We must be very careful when writing solutions with a string. This one is not preferable , except in some cases.

If you intend to perform some decimal operations , this would be preferable.

  string input_decimal_number = "1.50"; var regex = new System.Text.RegularExpressions.Regex("(?<=[\\.])[0-9]+"); if (regex.IsMatch(input_decimal_number)) { string decimal_places = regex.Match(input_decimal_number).Value; } 

// input: "1.05"
// output: "05"

// input: "2.50"
// output: "50"

// input: "0.0550"
// output: "0550"

You can find more about Regex at http://www.regexr.com/

-29
Oct. 15 '13 at 6:14
source share

best of the best ways:

 var floatNumber = 12.5523; var x = floatNumber - Math.Truncate(floatNumber); 

result you can convert but you like

+122
03 Sep '14 at 10:23
source share
 var decPlaces = (int)(((decimal)number % 1) * 100); 

This assumes your number has only two decimal places.

+62
Oct 23 '12 at 20:16
source share

The best way -

  double value = 10.567; int result = (int)((value - (int)value) * 100); Console.WriteLine(result); 

Output -

 56 
+9
Oct 23
source share

The easiest option, possibly with Math.truncate ()

 double value = 1.761 double decPart = value - Math.truncate(value) 
+7
Aug 05 '15 at 12:01
source share

Solution without rounding problem:

 double number = 10.20; var first2DecimalPlaces = (int)(((decimal)number % 1) * 100); Console.Write("{0:00}", first2DecimalPlaces); 

Outputs: 20

Note that if we did not produce the decimal number, it will print 19 .

also:

  • for 318.40 outputs: 40 (instead of 39 )
  • for 47.612345 outputs: 61 (instead of 612345 )
  • for 3.01 outputs: 01 (instead of 1 )

If you work with financial numbers, for example, if in this case you are trying to get a cent part of the transaction amount, always use the decimal data type.

Update:

The following will work if you treat it as a string (building a response to @SearchForKnowledge).

 10.2d.ToString("0.00", CultureInfo.InvariantCulture).Split('.')[1] 

Then you can use Int32.Parse to convert it to int.

+5
Jul 13 '15 at 18:47
source share
 int last2digits = num - (int) ((double) (num / 100) * 100); 
+1
Apr 02 '15 at 4:59
source share
  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; } 
0
Nov 10 '13 at 20:12
source share

Here is the extension method that I wrote for a similar situation. My application received numbers in the format 2.3 or 3.11, where the integer component of the number represented the years, and the fractional component represented the 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); } } 
0
Apr 05 '17 at 16:02 on
source share
 var result = number.ToString().Split(System.Globalization.NumberDecimalSeparator)[2] 

Returns it as a string (but you can always throw it back into int) and assumes the number has a value of "." somewhere.

-one
Oct 23 '12 at 20:18
source share

You can delete the point . from the double with which you are trying to get decimals using the Remove() function after converting double to string so that you can perform the required operations

Consider the presence of a double _Double value of 0.66781 , the following code will only show 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 can also use the Path class, which performs operations on string instances in cross-platform mode.

 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 
-one
Oct 23
source share

It is very simple

  float moveWater = Mathf.PingPong(theTime * speed, 100) * .015f; int m = (int)(moveWater); float decimalPart= moveWater -m ; Debug.Log(decimalPart); 
-one
Apr 18 '15 at 14:16
source share

Use regex: Regex.Match("\.(?\d+)") Someone will correct me if I am wrong here

-3
Oct 23 '12 at 20:22
source share

Why not use int y = value.Split('.')[1]; ?

The Split() function splits the value into separate content, and 1 displays the second value after .

-3
Jul 13 '15 at 19:33
source share



All Articles