Hallo,
hab _ ein Algorithmus geschrieben, dass eine Zahl X auf beliebig viele Nachkommastellen rundet:
Viel Spaß!
Post scriptum: Der MidPointRounding ist "AwayFromZero"!
hab _ ein Algorithmus geschrieben, dass eine Zahl X auf beliebig viele Nachkommastellen rundet:
C#-Quellcode
-
- public static float Round(float a, int n)
- {
- return (float)(((int)((Mod(((a + ((Mod((a * FasterPow(10, n + 1)), 10)) / FasterPow(10, n + 2))) * FasterPow(10, n + 1)), 10)) / 5) / FasterPow(10, n)) + ((a + ((Mod((a * FasterPow(10, n + 1)), 10)) / FasterPow(10, n + 2))) - (Mod(((a + ((Mod((a * FasterPow(10, n + 1)), 10)) / FasterPow(10, n + 2))) * FasterPow(10, n + 1)), 10)) / FasterPow(10, n + 1)));
- }
- private static double Mod(double a, double b)
- {
- return a - (b * (int)(a / b));
- }
- public static double FasterPow(double x, double y)
- {
- return Math.Exp(y * Math.Log(x));
- }
Viel Spaß!
Post scriptum: Der MidPointRounding ist "AwayFromZero"!
Und Gott alleine weiß alles am allerbesten und besser.