In C# code I have a floating point variable number that I want presented as a string but I want it formatted in a particular way. if I have a float number, like, 0.532492829839, for example and I want it represented in C# code as "53.24%", how would I do that? I imagine it would be done using IFormatProvidre and float.parse. But I have not done it before. First of all, I guess I should multiply the number by 10, but how would I pick a number of decimal place?

Recommended Answers

All 4 Replies

Member Avatar for Huntondoom
float A = 0.532492829839;
A = 100 * A
textbox1.text = string.format("00.00", A.ToString()) + "%"

I have not tested this but it should look something liks this I guess

textbox1.text = string.format("00.00", A.ToString()) + "%"

Why would you append a string to a string format (and your format is wrong :))

textbox1.Text = String.Format("{0:00.00}%", A.ToString());

You shouldn't be converting the float variable to a string. This will not allow the proper formatting:

double A = 0.532492829839;
string.Format("{0:0.00}%", A * 100.0);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.