Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break decimal value into two values (before and after decimal)

Tags:

c#

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

Problem : 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
like image 894
Faizan Avatar asked Dec 13 '22 06:12

Faizan


2 Answers

A 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 will ensure that the decimal separator will be a . and the split will be done in the right place. In my culture the decimal separator was a , for example

like image 162
Răzvan Flavius Panda Avatar answered Dec 14 '22 21:12

Răzvan Flavius Panda


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

like image 30
Haris Hasan Avatar answered Dec 14 '22 19:12

Haris Hasan