How do I convert the string “39.9983%” into “39%” in C#?

后端 未结 8 1050
一整个雨季
一整个雨季 2021-01-16 07:46

I don\'t want to do any rounding, straight up, \"39%\".

So \"9.99%\" should become \"9%\".

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-16 08:37

    string myPercentage = "48.8983%";
    
    string newString = myPercentage.Replace("%","");
    
    int newNumber = (int)Math.Truncate(Double.Parse(newString));
    
    Console.WriteLine(newNumber + "%");
    

    There maybe hundred ways of doing this and this is just one :)

提交回复
热议问题