jenik2205
jenik2205

Reputation: 475

How to change decimal separator in ExecuteReader c#

How to change decimal separator in string, e.g. in mnoz_obj item the returned value is 24,000 and I need to have 24.000. The values are from database to JSON. I tried ToString(new CultureInfo etc.) but this doesn't work. I expect that myString.Replace(",",".") is not correct way to do it.

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"
  }

Upvotes: 0

Views: 219

Answers (1)

dbraillon
dbraillon

Reputation: 1752

OdbcDataReader gives the value in its native format as stated in the doc.

You should then be able to cast it and use the overload of .ToString() you need.

Try something like:

((decimal)reader["mnoz_obj"]).ToString("N2")

Upvotes: 1

Related Questions