Skip to content

Commit

Permalink
Merge pull request #3 from NetFabric/StringFormat
Browse files Browse the repository at this point in the history
Stringformat
  • Loading branch information
aalmada committed May 6, 2016
2 parents 53d7f77 + dd49ed5 commit 4d2bfe2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
4 changes: 2 additions & 2 deletions NetFabric.Angle.Shared/Angle.Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ static int Compare(double d1, double d2)
return d1.CompareTo(d2);
}

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

string FormatString(double value, string format, IFormatProvider formatProvider)
static string FormatString(double value, string format, IFormatProvider formatProvider)
{
return value.ToString(format, formatProvider);
}
Expand Down
4 changes: 2 additions & 2 deletions NetFabric.Angle.Shared/Angle.NetMF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ static int Compare(double d1, double d2)
return double.CompareTo(d1, d2);
}

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

string FormatString(double value, string format, IFormatProvider formatProvider)
static string FormatString(double value, string format, IFormatProvider formatProvider)
{
return value.ToString(format);
}
Expand Down
13 changes: 8 additions & 5 deletions NetFabric.Angle.UnitTests/AngleTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
using System.Threading;

namespace NetFabric.UnitTests
{
Expand Down Expand Up @@ -530,6 +531,8 @@ public void LerpIsDefinedCorrectly()
[TestMethod]
public void ToStringIsDefinedCorrectly()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

Assert.AreEqual("3.14159265358979", Angle.Straight.ToString());

Assert.AreEqual("3.14159265358979", Angle.Straight.ToString("R"));
Expand Down Expand Up @@ -562,11 +565,11 @@ public void ToStringIsDefinedCorrectly()
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));
Assert.AreEqual("Radians: 3,14", String.Format(new CultureInfo("pt-PT"), "Radians: {0:R2}", Angle.Straight));
Assert.AreEqual("Degrees: 180,00", String.Format(new CultureInfo("pt-PT"), "Degrees: {0:D2}", Angle.Straight));
Assert.AreEqual("Degrees: 12° 34,56'", String.Format(new CultureInfo("pt-PT"), "Degrees: {0:M2}", Angle.FromDegrees(12, 34.56)));
Assert.AreEqual("Degrees: 12° 34' 56,78\"", String.Format(new CultureInfo("pt-PT"), "Degrees: {0:S2}", Angle.FromDegrees(12, 34, 56.78)));
Assert.AreEqual("Gradians: 200,00", String.Format(new CultureInfo("pt-PT"), "Gradians: {0:G2}", Angle.Straight));

}

Expand Down
6 changes: 2 additions & 4 deletions NetFabric.Angle.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
Implements a structure representing an angle.
The angle is represented internaly in radians but can be created and read in radians, degrees and gradians. Also supports arcminutes and arcseconds, useful for GPS coordinates.
The explicit declaration of the units in creation and reading methods reduces the tipical confusion when dealing with angles.
Includes reduction, reference angle, comparison, classification and trigonometry operations.
Includes linear interpolation, reduction, reference angle, comparison, classification and trigonometry operations.
</description>
<releaseNotes>
+ Added Lerp
+ Added SerializableAttribute when on .NET 3.5
+ Added AngleRangeAttribute
+ Added string format
</releaseNotes>
<copyright>2016 Antão Almada</copyright>
<language>en-US</language>
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,45 @@ var isAcute = Angle.IsAcute(Angle.FromDegrees(45.0)); // true
var isAcute = Angle.IsAcute(Angle.FromDegrees(-45.0)); // true
var isAcute = Angle.IsAcute(Angle.FromDegrees(315.0)); // false
```

### String format

The ToString() method supports the following formats:

```csharp
Angle.FromDegrees(45.0).ToString(); // 0.785398163397448
Angle.FromDegrees(45.0).ToString("R"); // 0.785398163397448
Angle.FromDegrees(45.0).ToString("D"); // 45
Angle.FromDegrees(45.0).ToString("M"); // 45° 0'
Angle.FromDegrees(45.0).ToString("S"); // 45° 0' 0"
Angle.FromDegrees(45.0).ToString("G"); // 50
```

Supports the number of decimal places:

```csharp
Angle.FromDegrees(45.0).ToString("R3"); // 0.785
Angle.FromDegrees(45.0).ToString("D3"); // 45.000
Angle.FromDegrees(45.0).ToString("M3"); // 45° 0.000'
Angle.FromDegrees(45.0).ToString("S3"); // 45° 0' 0.000"
Angle.FromDegrees(45.0).ToString("G3"); // 50.000
```

Supports culture (except for .NET Micro Framework):

```csharp
Angle.FromDegrees(45.0).ToString("R3", new CultureInfo("pt-PT")); // 0,785
Angle.FromDegrees(45.0).ToString("D3", new CultureInfo("pt-PT")); // 45,000
Angle.FromDegrees(45.0).ToString("M3", new CultureInfo("pt-PT")); // 45° 0,000'
Angle.FromDegrees(45.0).ToString("S3", new CultureInfo("pt-PT")); // 45° 0' 0,000"
Angle.FromDegrees(45.0).ToString("G3", new CultureInfo("pt-PT")); // 50,000
```

These formats can also be used in String.Format():

```csharp
String.Format("Radians: {0:R3}", Angle.FromDegrees(45.0)); // Radians: 0.785
String.Format("Degrees: {0:S3}", Angle.FromDegrees(45.0)); // Degrees: 45° 0' 0.000"
String.Format(new CultureInfo("pt-PT"), "Degrees: {0:S3}", Angle.FromDegrees(45.0)); // Degrees: 45° 0' 0,000"
```

0 comments on commit 4d2bfe2

Please sign in to comment.