Skip to content

Commit ff32edc

Browse files
authored
README: Revisit code samples with v4 code
Fix some compile errors, refine the examples a bit
1 parent de87678 commit ff32edc

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

README.md

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,25 @@ Run the following command in the [Package Manager Console](http://docs.nuget.org
4444
### <a name="static-typing"></a>Static Typing
4545

4646
```C#
47-
// Convert to the unit of choice - when you need it
48-
Mass weight = GetPersonWeight();
49-
Console.WriteLine("You weigh {0:0.#} kg.", weight.Kilograms);
50-
51-
// Avoid confusing conversions, such as between weight (force) and mass
52-
double weightNewtons = weight.Newtons; // No such thing
53-
54-
// Some popular conversions
47+
// Construct
5548
Length meter = Length.FromMeters(1);
56-
double cm = meter.Centimeters; // 100
57-
double yards = meter.Yards; // 1.09361
58-
double feet = meter.Feet; // 3.28084
59-
double inches = meter.Inches; // 39.3701
49+
Length twoMeters = new Length(2, LengthUnit.Meter);
50+
51+
// Convert
52+
double cm = meter.Centimeters; // 100
53+
double yards = meter.Yards; // 1.09361
54+
double feet = meter.Feet; // 3.28084
55+
double inches = meter.Inches; // 39.3701
56+
57+
// Pass quantity types instead of values to avoid conversion mistakes and communicate intent
58+
string PrintPersonWeight(Mass weight)
59+
{
60+
// No such thing! Weight in this context is Mass, not Force.
61+
double weightNewtons = weight.Newtons;
62+
63+
// Convert to the unit of choice - when you need it
64+
return $"You weigh {weight.Kilograms:F1} kg.";
65+
}
6066
```
6167

6268
### <a name="operator-overloads"></a>Operator Overloads
@@ -87,23 +93,23 @@ var usEnglish = new CultureInfo("en-US");
8793
var russian = new CultureInfo("ru-RU");
8894
var oneKg = Mass.FromKilograms(1);
8995

90-
// Honors Thread.CurrentUICulture
91-
Thread.CurrentUICulture = russian;
96+
// ToString() uses CurrentUICulture for abbreviation language and CurrentCulture for number formatting
97+
Thread.CurrentThread.CurrentUICulture = russian;
9298
string kgRu = oneKg.ToString(); // "1 кг"
9399
94-
// ToString() with specific culture and string format pattern
95-
string mgUs = oneKg.ToString(MassUnit.Milligram, usEnglish, "{1} {0:0.00}"); // "mg 1.00"
96-
string mgRu = oneKg.ToString(MassUnit.Milligram, russian, "{1} {0:0.00}"); // "мг 1,00"
100+
// ToString() with specific culture and custom string format pattern
101+
string mgUs = oneKg.ToUnit(MassUnit.Milligram).ToString(usEnglish, "unit: {1}, value: {0:F2}"); // "unit: mg, value: 1.00"
102+
string mgRu = oneKg.ToUnit(MassUnit.Milligram).ToString(russian, "unit: {1}, value: {0:F2}"); // "unit: мг, value: 1,00"
97103
98104
// Parse measurement from string
99-
Mass kg = Mass.Parse(usEnglish, "1.0 kg");
105+
Mass kg = Mass.Parse("1.0 kg", usEnglish);
100106

101107
// Parse unit from string, a unit can have multiple abbreviations
102-
RotationalSpeedUnit rpm1 == RotationalSpeed.ParseUnit("rpm"); // RotationalSpeedUnit.RevolutionPerMinute
103-
RotationalSpeedUnit rpm2 == RotationalSpeed.ParseUnit("r/min"); // RotationalSpeedUnit.RevolutionPerMinute
108+
RotationalSpeedUnit rpm1 = RotationalSpeed.ParseUnit("rpm"); // RotationalSpeedUnit.RevolutionPerMinute
109+
RotationalSpeedUnit rpm2 = RotationalSpeed.ParseUnit("r/min"); // RotationalSpeedUnit.RevolutionPerMinute
104110
105-
// Get default abbreviation for a unit
106-
string abbrevKg = Mass.GetAbbreviation(MassUnit.Kilogram); // "kg"
111+
// Get default abbreviation for a unit, the first if more than one is defined in Length.json for Kilogram unit
112+
string kgAbbreviation = Mass.GetAbbreviation(MassUnit.Kilogram); // "kg"
107113
```
108114

109115
#### Gotcha: AmbiguousUnitParseException
@@ -113,6 +119,14 @@ Unfortunately there is no built-in way to avoid this, either you need to ensure
113119
Example:
114120
`Length.Parse("1 pt")` throws `AmbiguousUnitParseException` with message `Cannot parse "pt" since it could be either of these: DtpPoint, PrinterPoint`.
115121

122+
### Dynamically Parsing Quantities and Units
123+
Sometimes you need to work with quantities and units at runtime, such as parsing user input.
124+
There are three classes to help with this:
125+
126+
```c#
127+
128+
```
129+
116130

117131
### <a name="example-app"></a>Example: Creating a dynamic unit converter app
118132
[Source code](https://github.com/angularsen/UnitsNet/tree/master/Samples/UnitConverter.Wpf) for `Samples/UnitConverter.Wpf`<br/>
@@ -140,10 +154,6 @@ LengthUnit[] lengthUnits = Length.Units;
140154
double inputValue; // Obtain from textbox
141155
LengthUnit fromUnit, toUnit; // Obtain from ListBox selections
142156
double resultValue = Length.From(inputValue, fromUnit).As(toUnit);
143-
144-
// Alternatively, you can also convert using string representations of units
145-
double centimeters = UnitConverter.ConvertByName(5, "Length", "Meter", "Centimeter"); // 500
146-
double centimeters2 = UnitConverter.ConvertByAbbreviation(5, "Length", "m", "cm"); // 500
147157
```
148158

149159
### Example: WPF app using IValueConverter to parse quantities from input

0 commit comments

Comments
 (0)