shithub: util

Download patch

ref: 6ab15be07c62e3d31c916084a377ff3b6ba7d0ad
parent: 8027983841965d3dd3ea21c224db7271656cbc8e
author: eli <eli@owl>
date: Sat Jul 5 13:25:07 EDT 2025

program with function for rounding a number

--- /dev/null
+++ b/round.c
@@ -1,0 +1,34 @@
+#include <u.h>
+#include <libc.h>
+
+float round(float in) {
+	float f;
+
+	f = fmod(in, 1.0);
+
+	if (in > 0) {
+		if (f < 0.5)
+			return floor(in);
+		return ceil(in);
+	}
+
+	if (f > -0.5)
+		return ceil(in);
+	return floor(in);
+}
+
+void main() {
+	char buf[1024];
+	int r;
+	float f;
+
+	r = read(0, buf, sizeof(buf));
+	if (r <= 0)
+		return;
+
+	buf[r] = '\0';
+
+	f = round(atof(buf));
+
+	print("%f\n", f);
+}
--