Reputation: 1526
Consider the following code:
int integralPart = 123;
int decimalPart = 12345;
// double desiredDouble = 123.12345;
I would like to create a double from 2 ints, as shown in the example.
I know I can use double.Parse(integralPart.ToString() + "." + decimalPart.ToString())
, but I am getting some exceptions if the application isn't using English as default language.
Upvotes: 1
Views: 743
Reputation: 393557
From your wording alone, I'd suggest you really wanted to use decimal
s:
int integralPart = 123;
int decimalPart = 12345;
decimal result = decimalPart;
while (result>=1m) result/=10m; // CAVEAT: see below
result+=integralPart;
Oops. And there is the big ambiguity problem others have mentioned. Likely, you'd need to replace my while
with a fixed scale:
result = integralPart + decimalPart / 1000000m; // fixed scale factor
Upvotes: 2
Reputation: 22804
A fix to your solution:
double.Parse(integralPart.ToString() + "." + decimalPart.ToString(),
CultureInfo.InvariantCulture);
It's not very efficient, though.
If they are prices, use decimal
instead:
decimal.Parse(integralPart.ToString() + "." + decimalPart.ToString(),
CultureInfo.InvariantCulture);
Upvotes: 2