Split the decimal value into two values โ€‹โ€‹(before and after the decimal)

How to break a decimal value into integer values, the first value should be the value before decimal, and the other value after decimal.

Problem: the decimal place is unknown, as well as the number of digits; ex:

double value = 2635.215; int firstValue = 2635; // Should be int secondValue = 215; // Should be 
+4
source share
6 answers

You can use the String.Split method to split the string. Convert double to string and then split it based on .

+2
source

Possible Solution:

 using System.Globalization; namespace DecimalSplit { class Program { static void Main(string[] args) { double value = 2635.215; var values = value.ToString(CultureInfo.InvariantCulture).Split('.'); int firstValue = int.Parse(values[0]); int secondValue = int.Parse(values[1]); } } } 

Using CultureInfo.InvariantCulture when converting to String ensures that the decimal separator will be . , and the split will be performed in the right place. In my culture, the decimal separator was , for example

+8
source

This problem is unsolvable because a double value can have an extremely large number of decimal digits, so you cannot guarantee the ability to represent them in integer sizes. The closest I can give you is that it works not with integers, but with doubles:

 double left = System.Math.Floor(value); double right = left - value; 
+2
source

Below is a little information about the Rฤƒzvan Panda example.

The exception was expressed in int secondValue = int.Parse(values[1]); if we analyze a value without a decimal sum.

Another point is that it is better to have a string for the second value. See Example 3.

 static void Main(string[] args) { double val1 = 2635.215; Console.Out.WriteLine(GetFirstValue(val1)); // out 2635 Console.Out.WriteLine(GetSecondValue(val1)); // out 215 double val2 = 2; Console.Out.WriteLine(GetFirstValue(val2)); // out 2 Console.Out.WriteLine(GetSecondValue(val2)); // out '' double val3 = 3.04; Console.Out.WriteLine(GetFirstValue(val3)); // out 3 Console.Out.WriteLine(GetSecondValue(val3)); // out 04 } public static string GetFirstValue(double value) { var values = value.ToString(CultureInfo.InvariantCulture).Split('.'); return values[0]; } public static string GetSecondValue(double value) { var values = value.ToString(CultureInfo.InvariantCulture).Split('.'); return values.Length > 1 ? values[1] : string.Empty; } 
+1
source

to try:

  double value = 2635.215; string a = value.ToString(); string[] b =a.Split('.'); int firstValue= int.Parse(b[0]); int secondValue= int.Parse(b[1]); 
0
source

You can do:

 double decimalNumber = 123.456; var numbersBeforeDecimalPoint = Math.Truncate(decimalNumber); var numbersAfterDecimalPoint = decimalNumber - numbersBeforeDecimalPoint; 

Using Math.Truncate , we can only get integers, and we can use this in tandem with our original number to get the decimal part.
As a result, you get 123 and 0.456 .

Not sure if this is exactly what you need (I appreciate that you might want 123 and 456 ). There are many good suggestions here for string manipulation if you need more precise formatting.

0
source

Source: https://habr.com/ru/post/1389249/


All Articles