String convert to float ore decimal type with 0 decimals
C#
Radni = rd.GetString("R1")String convert to float ore decimal type with 0 decimals
Radni = rd.GetString("R1")Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Nov 2, 2023, 7:42 AM EDT
To convert a string to a float or decimal type with 0 decimal places, you can use the
float.Parseordecimal.Parsemethod and then format the result with zero decimal places.string Radni = rd.GetString("R1"); // Assuming Radni is a string containing a numeric value if (float.TryParse(Radni, out float floatValue)) { // Convert to float with 0 decimal places string formattedFloat = floatValue.ToString("F0"); Console.WriteLine("Float with 0 decimal places: " + formattedFloat); } else if (decimal.TryParse(Radni, out decimal decimalValue)) { // Convert to decimal with 0 decimal places string formattedDecimal = decimalValue.ToString("F0"); Console.WriteLine("Decimal with 0 decimal places: " + formattedDecimal); } else { Console.WriteLine("Failed to parse the input as a valid float or decimal."); }Jignesh KumarRank #91 · 22.9K reputationPosted Nov 3, 2023, 12:22 AM EDT
Hello Goran Bibic,
Please use this one,
string Radni = rd.GetString("R1"); if (float.TryParse(Radni, out float result)) { float roundedValue = (float)Math.Round(result, 0); decimal decimalValue = (decimal)Math.Round(result, 0); }