Skip to content

Commit

Permalink
Merge pull request #2 from NetFabric/StringFormat
Browse files Browse the repository at this point in the history
Added String format
  • Loading branch information
aalmada committed May 6, 2016
2 parents 2376dbb + aaa0fc8 commit 53d7f77
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 4 deletions.
27 changes: 27 additions & 0 deletions NetFabric.Angle.Shared/Angle.Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public partial struct Angle
: IEquatable<Angle>
, IComparable
, IComparable<Angle>
, IFormattable
{

/// <summary>
Expand All @@ -32,17 +33,43 @@ public int CompareTo(object obj)
return this.CompareTo((Angle)obj);
}

/// <summary>
/// Converts the value of the current Angle object to its equivalent string representation using the specified format and culture-specific format information.
/// </summary>
/// <param name="format">A standard or custom date and time format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <returns>A string representation of value of the current Angle object as specified by format and provider.</returns>
public string ToString(string format, IFormatProvider formatProvider)
{
return FormatString(format, formatProvider);
}

static int Compare(double d1, double d2)
{
return d1.CompareTo(d2);
}

string FormatString(int value, string format, IFormatProvider formatProvider)
{
return value.ToString(format, formatProvider);
}

string FormatString(double value, string format, IFormatProvider formatProvider)
{
return value.ToString(format, formatProvider);
}

static void ThrowArgumentOutOfRange(string paramName, object paramValue, string message)
{
throw new ArgumentOutOfRangeException(
paramName,
paramValue,
message);
}

static void ThrowFormatException(string format)
{
throw new FormatException(String.Format("The '{0}' format string is not supported.", format));
}
}
}
15 changes: 15 additions & 0 deletions NetFabric.Angle.Shared/Angle.NetMF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ static int Compare(double d1, double d2)
return double.CompareTo(d1, d2);
}

string FormatString(int value, string format, IFormatProvider formatProvider)
{
return value.ToString(format);
}

string FormatString(double value, string format, IFormatProvider formatProvider)
{
return value.ToString(format);
}

static void ThrowArgumentOutOfRange(string paramName, object paramValue, string message)
{
throw new ArgumentOutOfRangeException(
paramName,
message);
}

static void ThrowFormatException(string format)
{
throw new Exception("The '" + format + "' format string is not supported.");
}
}
}
67 changes: 63 additions & 4 deletions NetFabric.Angle.Shared/Angle.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;

namespace NetFabric
{
Expand Down Expand Up @@ -770,9 +771,67 @@ public static Angle Divide(Angle left, double right)
return new Angle(left.radians / right);
}

#endregion
#endregion

#region string format

/// <summary>
/// Converts the value of the current Angle object to its equivalent string representation, using a specified format.
/// </summary>
/// <param name="format">A string that specifies the format to be used for the returned string.</param>
/// <returns>A string representation of the value of the current Angle object, in the specified format.</returns>
public string ToString(string format)
{
return FormatString(format, null);
}

string FormatString(string format, IFormatProvider formatProvider)
{
if (format == null || format.Length == 0)
format = "R";

#region object overrides
format = format.Trim().ToUpper();

var doubleFormat = "G";
if (format.Length > 1)
doubleFormat = "N" + format.Substring(1);

switch (format[0])
{
case 'R':
return FormatString(ToRadians(), doubleFormat, formatProvider);
case 'D':
return FormatString(ToDegrees(), doubleFormat, formatProvider);
case 'M':
{
int degrees;
double minutes;
ToDegrees(out degrees, out minutes);
return FormatString(degrees, "G", formatProvider) + "° " +
FormatString(minutes, doubleFormat, formatProvider) + "'";
}
case 'S':
{
int degrees;
int minutes;
double seconds;
ToDegrees(out degrees, out minutes, out seconds);
return FormatString(degrees, "G", formatProvider) + "° " +
FormatString(minutes, "G", formatProvider) + "' " +
FormatString(seconds, doubleFormat, formatProvider) + "\"";
}
case 'G':
return FormatString(ToGradians(), doubleFormat, formatProvider);
default:
ThrowFormatException("The '" + format + "' format string is not supported.");
break;
}
throw new InvalidOperationException();
}

#endregion

#region object overrides

/// <summary>
/// Returns a value indicating whether this instance is equal to a specified object.
Expand Down Expand Up @@ -801,10 +860,10 @@ public override int GetHashCode()
/// <returns>A string that represents the current object.</returns>
public override string ToString()
{
return radians.ToString();
return ToString(null);
}

#endregion
#endregion

}
}
44 changes: 44 additions & 0 deletions NetFabric.Angle.UnitTests/AngleTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;

namespace NetFabric.UnitTests
{
Expand Down Expand Up @@ -526,5 +527,48 @@ public void LerpIsDefinedCorrectly()
Assert.AreEqual(Angle.FromDegrees(0.0), Angle.Lerp(Angle.FromDegrees(135.0), Angle.FromDegrees(45.0), 1.5));
}

[TestMethod]
public void ToStringIsDefinedCorrectly()
{
Assert.AreEqual("3.14159265358979", Angle.Straight.ToString());

Assert.AreEqual("3.14159265358979", Angle.Straight.ToString("R"));
Assert.AreEqual("180", Angle.Straight.ToString("D"));
Assert.AreEqual("12° 34.56'", Angle.FromDegrees(12, 34.56).ToString("M"));
Assert.AreEqual("12° 34' 56.7800000000018\"", Angle.FromDegrees(12, 34, 56.78).ToString("S"));
Assert.AreEqual("200", Angle.Straight.ToString("G"));

Assert.AreEqual("3,14159265358979", Angle.Straight.ToString("R", new CultureInfo("pt-PT")));
Assert.AreEqual("180", Angle.Straight.ToString("D", new CultureInfo("pt-PT")));
Assert.AreEqual("12° 34,56'", Angle.FromDegrees(12, 34.56).ToString("M", new CultureInfo("pt-PT")));
Assert.AreEqual("12° 34' 56,7800000000018\"", Angle.FromDegrees(12, 34, 56.78).ToString("S", new CultureInfo("pt-PT")));
Assert.AreEqual("200", Angle.Straight.ToString("G", new CultureInfo("pt-PT")));

Assert.AreEqual("Radians: 3.14159265358979", String.Format("Radians: {0:R}", Angle.Straight));
Assert.AreEqual("Degrees: 180", String.Format("Degrees: {0:D}", Angle.Straight));
Assert.AreEqual("Degrees: 12° 34.56'", String.Format("Degrees: {0:M}", Angle.FromDegrees(12, 34.56)));
Assert.AreEqual("Degrees: 12° 34' 56.7800000000018\"", String.Format("Degrees: {0:S}", Angle.FromDegrees(12, 34, 56.78)));
Assert.AreEqual("Gradians: 200", String.Format("Gradians: {0:G}", Angle.Straight));

Assert.AreEqual("3.14", Angle.Straight.ToString("R2"));
Assert.AreEqual("180.00", Angle.Straight.ToString("D2"));
Assert.AreEqual("12° 34.56'", Angle.FromDegrees(12, 34.56).ToString("M2"));
Assert.AreEqual("12° 34' 56.78\"", Angle.FromDegrees(12, 34, 56.78).ToString("S2"));
Assert.AreEqual("200.00", Angle.Straight.ToString("G2"));

Assert.AreEqual("3,14", Angle.Straight.ToString("R2", new CultureInfo("pt-PT")));
Assert.AreEqual("180,00", Angle.Straight.ToString("D2", new CultureInfo("pt-PT")));
Assert.AreEqual("12° 34,56'", Angle.FromDegrees(12, 34.56).ToString("M2", new CultureInfo("pt-PT")));
Assert.AreEqual("12° 34' 56,78\"", Angle.FromDegrees(12, 34, 56.78).ToString("S2", new CultureInfo("pt-PT")));
Assert.AreEqual("200,00", Angle.Straight.ToString("G2", new CultureInfo("pt-PT")));

Assert.AreEqual("Radians: 3.14", String.Format("Radians: {0:R2}", Angle.Straight));
Assert.AreEqual("Degrees: 180.00", String.Format("Degrees: {0:D2}", Angle.Straight));
Assert.AreEqual("Degrees: 12° 34.56'", String.Format("Degrees: {0:M2}", Angle.FromDegrees(12, 34.56)));
Assert.AreEqual("Degrees: 12° 34' 56.78\"", String.Format("Degrees: {0:S2}", Angle.FromDegrees(12, 34, 56.78)));
Assert.AreEqual("Gradians: 200.00", String.Format("Gradians: {0:G2}", Angle.Straight));

}

}
}

0 comments on commit 53d7f77

Please sign in to comment.