Round Double To N Decimal Places C

[Solved] Round Double To N Decimal Places C | Perl - Code Explorer | yomemimo.com
Question : round decimal point in C

Answered by : phillyyy

#include <math.h>
float val = 37.777779;
float rounded_down = floorf(val * 100) / 100; /* Result: 37.77 */
float nearest = roundf(val * 100) / 100; /* Result: 37.78 */
float rounded_up = ceilf(val * 100) / 100; /* Result: 37.78 */

Source : https://stackoverflow.com/questions/1343890/how-do-i-restrict-a-float-value-to-only-two-places-after-the-decimal-point-in-c | Last Update : Wed, 05 Oct 22

Question : Rounding Floating Point Number To two Decimal Places in C

Answered by : nishit-dua

#include <iostream>
using namespace std;
int main()
{
    float var = 37.66666;
 
    // Directly print the number with .2f precision
    printf("%.2f", var);
    return 0;
}

Source : https://www.geeksforgeeks.org/rounding-floating-point-number-two-decimal-places-c-c/ | Last Update : Sat, 12 Mar 22

Answers related to round double to n decimal places c

Code Explorer Popular Question For Perl