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
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
You can use String.Split
method for splitting a string. Convert double
to string and then split it based on .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With