Search code examples
c++c++-cli

Rounding float in ToString() function?


So I have a form with a label which is supposed to display float value, the problem is that I need to have that number rounded to 2 decimal places whatever happens:

label1->Text = System::Convert::ToString( (float)((float)temperature/204.6) );

I tried looking for few hours but as I found there is no method for direct rounding of that beast equation and as far as I know no way to tell ToString() to round the thing to 2 decimals.

Is there any easy way to round the result to 2 decimal places inside ToString method?


Solution

  • This is quite simple to do:

    String^ s = String::Format("{0:N2}", temperature/204.6);