如何在 ExecuteReader c# 中更改小数点分隔符
How to change decimal separator in ExecuteReader c#
如何更改字符串中的小数点分隔符,例如在 mnoz_obj 项中,返回值为 24,000,我需要 24.000。这些值是从数据库到 JSON。
我尝试了 ToString(new CultureInfo etc.) 但这不起作用。我希望 myString.Replace(",",".") 不是正确的方法。
public static string getDoklad()
{
var dbCon = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
string[] fileArguments = Environment.GetCommandLineArgs();
List<ZebraPolozky> zebraPolozky = new List<ZebraPolozky>();
using (var cn = new OdbcConnection(dbCon))
{
OdbcCommand cmd = cn.CreateCommand();
cmd.CommandText = "SELECT * FROM cis06zebrap";
cn.Open();
using (var reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
ZebraPolozky zebraPolozka = new ZebraPolozky
{
doklad = reader["doklad"].ToString(),
sklad = reader["sklad"].ToString(),
reg = reader["reg"].ToString(),
mnoz_obj = reader["mnoz_obj"].ToString(),
mnoz_vyd = reader["mnoz_vyd"].ToString(),
kc_pce = reader["kc_pce"].ToString(),
sarze = reader["sarze"].ToString(),
datspo = reader["datspo"].ToString(),
veb = reader["veb"].ToString(),
poc2 = reader["poc2"].ToString(),
pvp06pk = reader["pvp06pk"].ToString(),
znacky = reader["znacky"].ToString(),
stav = reader["stav"].ToString(),
//prac = reader["prac"].ToString(),
//exp = reader["exp"].ToString()
};
zebraPolozky.Add(zebraPolozka);
}
}
}
cn.Close();
}
//var collw = new { polozky = zebraPolozky };
var jsonString = JsonConvert.SerializeObject(zebraPolozky);
return jsonString;
}
{
"doklad": "568375",
"sklad": "901",
"reg": "185121",
"mnoz_obj": "24,000",
"mnoz_vyd": "0,000",
"kc_pce": "240,72",
"sarze": "",
"datspo": "",
"veb": "24,00",
"poc2": "1",
"pvp06pk": "116783437",
"znacky": "R1902",
"stav": "0"
}
OdbcDataReader
以 doc.
中所述的原始格式给出值
然后您应该能够转换它并使用您需要的 .ToString()
重载。
试试这样的东西:
((decimal)reader["mnoz_obj"]).ToString("N2")
如何更改字符串中的小数点分隔符,例如在 mnoz_obj 项中,返回值为 24,000,我需要 24.000。这些值是从数据库到 JSON。 我尝试了 ToString(new CultureInfo etc.) 但这不起作用。我希望 myString.Replace(",",".") 不是正确的方法。
public static string getDoklad()
{
var dbCon = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
string[] fileArguments = Environment.GetCommandLineArgs();
List<ZebraPolozky> zebraPolozky = new List<ZebraPolozky>();
using (var cn = new OdbcConnection(dbCon))
{
OdbcCommand cmd = cn.CreateCommand();
cmd.CommandText = "SELECT * FROM cis06zebrap";
cn.Open();
using (var reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
ZebraPolozky zebraPolozka = new ZebraPolozky
{
doklad = reader["doklad"].ToString(),
sklad = reader["sklad"].ToString(),
reg = reader["reg"].ToString(),
mnoz_obj = reader["mnoz_obj"].ToString(),
mnoz_vyd = reader["mnoz_vyd"].ToString(),
kc_pce = reader["kc_pce"].ToString(),
sarze = reader["sarze"].ToString(),
datspo = reader["datspo"].ToString(),
veb = reader["veb"].ToString(),
poc2 = reader["poc2"].ToString(),
pvp06pk = reader["pvp06pk"].ToString(),
znacky = reader["znacky"].ToString(),
stav = reader["stav"].ToString(),
//prac = reader["prac"].ToString(),
//exp = reader["exp"].ToString()
};
zebraPolozky.Add(zebraPolozka);
}
}
}
cn.Close();
}
//var collw = new { polozky = zebraPolozky };
var jsonString = JsonConvert.SerializeObject(zebraPolozky);
return jsonString;
}
{
"doklad": "568375",
"sklad": "901",
"reg": "185121",
"mnoz_obj": "24,000",
"mnoz_vyd": "0,000",
"kc_pce": "240,72",
"sarze": "",
"datspo": "",
"veb": "24,00",
"poc2": "1",
"pvp06pk": "116783437",
"znacky": "R1902",
"stav": "0"
}
OdbcDataReader
以 doc.
然后您应该能够转换它并使用您需要的 .ToString()
重载。
试试这样的东西:
((decimal)reader["mnoz_obj"]).ToString("N2")