Parse bilan bir juft c # ichida ikkita int ni birlashtirish

Ikki int ni bir noyob juftlik bilan birlashtirish kerak. Mening kodim va misol:

int left = 1;
int right = 38;

Ushbu natijani ikki barobarga oshirishni xohlayman:

ikki marta birlashma = 1.38

Qanday qilib bir juftlik ichida ikki int birlashtirilishi mumkin?

Buni sinab ko'rdim, lekin ishlamayapti

double result = double.Parse(left.ToString() + "." + right.ToString());

Rahmat

0
@DmitryBychenko ta'kidlaganidek, siz ham left.ToString (CultureInfo.CurrentCulture) + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparat & zwnj; yoki + right.ToString (CultureInfo.CurrentCulture); foydalanishingiz mumkin. madaniyat o'rniga,
qo'shib qo'ydi muallif Alessandro D'Andria, manba

6 javoblar

Ehtimol, CultureInfo.InvariantCulture ni belgilang.

  int left = 1;
  int right = 38;

  double result = double.Parse(
     left.ToString() + "." + right.ToString(),
     CultureInfo.InvariantCulture);

joriy madaniyatdan foydalangan holda ( o'zboshimchali kasrni ajratuvchi bo'lishi mumkin, masalan, rus tilida , )

4
qo'shib qo'ydi

Siz ushbu yondashuvni sinab ko'rishingiz va konversiyalash natijasini yoki konvertatsiya muvaffaqiyatli bo'lishiga erishishingiz mumkin.

try
{
    int left = 1;
    int right = 38;
    double Both;
    bool ConverstionSucceed = double.TryParse((left + "." + right), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out Both);
}
catch (Exception ex)
{
} 
1
qo'shib qo'ydi

Bu ham ishlaydi:

int left = 1;
int right = 38;

double result = left + right/Math.Pow(10, right.ToString().Length);
0
qo'shib qo'ydi
@DmitryBychenko Ha, siz haqsiz.
qo'shib qo'ydi muallif Siamak Ferdos, manba
right.Length == 2 bo'lsa, Math.Pow emas, balki Math.Pow /code>
qo'shib qo'ydi muallif Dmitry Bychenko, manba

Like this answer: answer

Shuningdek, interpolatsiyalangan simlar bilan (agar ulardan foydalanish imkoniyati mavjud bo'lsa) variant juda qisqacha ko'rinadi:

int left = 1;
int right = 38;

double result = double.Parse($"{left}.{right}", CultureInfo.InvariantCulture);
0
qo'shib qo'ydi

Ushbu usul joriy madaniyatdan o'nlikni ajratuvchi belgini qo'shadi.

        int left = 1;
        int right = 38;

        double result = Convert.ToDouble(
           left.ToString() + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator + right.ToString());
0
qo'shib qo'ydi

Tugmani satrlarga aylantira olasiz, uni stragarlarga birlashtirasiz va uni er-xotinga aylantira olasiz.

 left.toString();
 right.toString();
 string combinedLeftRight = left + "." + right;
 double mergeVariable = Convert.ToDouble(combinedLeftRight);

greetz

0
qo'shib qo'ydi
Kodingizda Convert.ToDouble kodi Culture.CurrentCulture dan foydalanadi; Iltimos, CultureInfo.InvariantCulture ni ko'rsating
qo'shib qo'ydi muallif Dmitry Bychenko, manba