简体   繁体   中英

convert datarow string to decimal in c#

i have a datatable that comes form a excel file but it seem that it's impossible to convert text field into decimal with dot instead of comma.

i have tried using culture but result is with comma or without punctation

for example "4,65" become "465" or "4,65" but not "4.65"

strange thinks is that this code is work

var price = decimal.Parse("4,65", new NumberFormatInfo() { NumberDecimalSeparator = "," });

price = 4.65

but this not work

var price = decimal.Parse(excelRow[0][7].ToString(), new NumberFormatInfo() { NumberDecimalSeparator = "," });

price = "4,65";

this is other trial

decimal price = Convert.ToDecimal(excelRow[0][7].ToString());
var price =decimal.Parse(excelRow[0][7].ToString().Replace(',', '.'), CultureInfo.InvariantCulture);
ar price =decimal.Parse(excelRow[0][7].ToString(), new CultureInfo("en-US"));
decimal price =(decimal)excelRow[0][7];
string price = String.Format("{0:0.##}", (decimal) excelRow[0][7]); 
string sPrice = excelRow[0][7].ToString();
decimal price = decimal.Parse(sPrice, new NumberFormatInfo() { NumberDecimalSeparator = "," });

you can try this

using System;
using System.Globalization;

class Test
{
    static void Main()        
    {
        decimal d = decimal.Parse("1200.00", CultureInfo.InvariantCulture);
        Console.WriteLine(d.ToString(CultureInfo.InvariantCulture));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM