简体   繁体   中英

String Formatting in C# to handle decimal places

In the Web API there is a Number data type field we retrieve from oracle DB and return in JSON format. Since it is of Number type it shows in the result 364578.0 so now using the string format we are trying to avoid the decimal point like

 serializer.Serialize(writer, string.Format("{0:n0}",reader[i]));

It did solve the issue, it does return the result omitting the decimal spaces but shows result like 364,578 I am just checking if there is any possibility that we can return the result like 364578

Try

serializer.Serialize(writer, string.Format("{0:f0}",reader[i]));

The F format does not use thousand separators. Check MSDN for details.

您可以使用自定义格式

serializer.Serialize(writer, (decimal)reader[i].ToString("0.#");

We had a little discussion in comments. You don't seem to be able to fix the problem since the API is out of your hands. That means that you have to workaround it.

From your question I understand the other end expects an integer instead of a decimal. Okay, well let's give it an int then:

serializer.Serialize(writer, Convert.ToInt32(reader[i]));

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