String format for only one decimal place?

前端 未结 6 1675
天命终不由人
天命终不由人 2020-12-10 10:44

I\'d like to dispaly only one decimal place. I\'ve tried the following:

string thevalue = \"6.33\";
thevalue = string.Format(\"{0:0.#}\", thevalue);
         


        
6条回答
  •  攒了一身酷
    2020-12-10 11:11

    You need it to be a floating-point value for that to work.

    double thevalue = 6.33;
    

    Here's a demo. Right now, it's just a string, so it'll be inserted as-is. If you need to parse it, use double.Parse or double.TryParse. (Or float, or decimal.)

提交回复
热议问题