How to round a float to 2 decimal places in C#

2 Answers

0 votes
using System;

class Program
{
    static void Main() {
        float f = 356.8719533f;
        
        string s = f.ToString ("0.##");
    
        Console.Write(s);
    }
}



/*
run:

356.87

*/

 



answered Sep 4, 2019 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        float f = 356.8719533f;
        
        f = (float)Math.Round(f * 100f) / 100f;
    
        Console.Write(f);
    }
}



/*
run:

356.87

*/

 



answered Sep 4, 2019 by avibootz

Related questions

1 answer 157 views
1 answer 257 views
4 answers 122 views
1 answer 172 views
3 answers 242 views
3 answers 300 views
...