Rebracer update.
1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="FractionHelper.cs" company="OxyPlot">
3 // The MIT License (MIT)
5 // Copyright (c) 2012 Oystein Bjorke
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // Generates fraction strings from double values.
29 // --------------------------------------------------------------------------------------------------------------------
33 using System.Globalization;
36 /// Provides functionality to generate fraction strings from double values.
39 /// Examples: "3/4", "PI/2"
41 public static class FractionHelper
44 /// Converts a double to a fraction string.
46 /// <param name="value">
49 /// <param name="unit">
52 /// <param name="unitSymbol">
55 /// <param name="eps">
58 /// <param name="formatProvider">
59 /// The format Provider.
62 /// The convert to fraction string.
64 public static string ConvertToFractionString(
67 string unitSymbol = null,
69 IFormatProvider formatProvider = null)
71 if (Math.Abs(value) < eps)
79 // int whole = (int)(value - (int) value);
81 // int frac = (int) ((value - whole)*N);
82 // var d = GCF(N,frac);
83 for (int d = 1; d <= 64; d++)
86 var ni = (int)Math.Round(n);
87 if (Math.Abs(n - ni) < eps)
89 string nis = unitSymbol == null || ni != 1 ? ni.ToString(CultureInfo.InvariantCulture) : string.Empty;
92 return string.Format("{0}{1}", nis, unitSymbol);
95 return string.Format("{0}{1}/{2}", nis, unitSymbol, d);
99 return string.Format(formatProvider ?? CultureInfo.CurrentCulture, "{0}{1}", value, unitSymbol);
103 /// Finds the greates common divisor.
114 public static int gcd(int a, int b)
121 return gcd(b, a % b);
125 /// Finds the greatest common factor.
136 private static int GCF(int x, int y)