String format for only one decimal place?

前端 未结 6 1685
天命终不由人
天命终不由人 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:38

    option 1 (let it be string):

    string thevalue = "6.33";
    thevalue = string.Format("{0}", thevalue.Substring(0, thevalue.length-1));
    

    option 2 (convert it):

    string thevalue = "6.33";
    var thevalue = string.Format("{0:0.0}", double.Parse(theValue));
    

    option 3 (fire up RegEx):

    var regex = new Regex(@"(\d+\.\d)"); // but that everywhere, maybe static
    thevalue = regexObj.Match(thevalue ).Groups[1].Value;
    

提交回复
热议问题