You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As fallback if no culture is specified. This allows
the user to specify a culture other than CurrentUICulture
and fixes tests where rely on that to test default
behavior.
// Windows Runtime Component does not support CultureInfo type, so use culture name string instead for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
79
-
#if WINDOWS_UWP
80
-
using Culture = System.String;
81
-
#else
82
-
using Culture = System.IFormatProvider;
83
-
#endif
84
-
85
78
// ReSharper disable once CheckNamespace
86
79
87
80
namespace UnitsNet
@@ -293,12 +286,29 @@ namespace UnitsNet
293
286
/// Get unit abbreviation string.
294
287
/// </summary>
295
288
/// <param name="unit">Unit to get abbreviation for.</param>
296
-
/// <param name="culture">Culture to use for localization. Defaults to Thread.CurrentUICulture.</param>
289
+
#if WINDOWS_UWP
290
+
/// <param name="cultureName">Name of culture (ex: "en-US") to use for localization. Defaults to <see cref="UnitSystem" />'s default culture.</param>
291
+
#else
292
+
/// <param name="provider">Format to use for localization. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
293
+
#endif
297
294
/// <returns>Unit abbreviation string.</returns>
298
295
[UsedImplicitly]
299
-
public static string GetAbbreviation($unitEnumName unit, [CanBeNull] Culture culture)
// Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
/// Parse a string with one or two quantities of the format "<quantity> <unit>".
499
509
/// </summary>
500
510
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
501
-
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
511
+
#if WINDOWS_UWP
512
+
/// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="UnitSystem" />'s default culture.</param>
513
+
#else
514
+
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
515
+
#endif
502
516
/// <example>
503
517
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
504
518
/// </example>
@@ -517,17 +531,24 @@ namespace UnitsNet
517
531
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
518
532
/// Units.NET exceptions from other exceptions.
519
533
/// </exception>
520
-
public static $quantityName Parse(string str, [CanBeNull] Culture culture)
534
+
public static $quantityName Parse(
535
+
string str,
536
+
#if WINDOWS_UWP
537
+
[CanBeNull] string cultureName)
538
+
#else
539
+
[CanBeNull] IFormatProvider provider)
540
+
#endif
521
541
{
522
542
if (str == null) throw new ArgumentNullException("str");
523
543
524
-
// Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx
// Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
/// Try to parse a string with one or two quantities of the format "<quantity> <unit>".
554
575
/// </summary>
555
576
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
556
-
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
577
+
#if WINDOWS_UWP
578
+
/// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="UnitSystem" />'s default culture.</param>
579
+
#else
580
+
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
581
+
#endif
557
582
/// <param name="result">Resulting unit quantity if successful.</param>
558
583
/// <example>
559
584
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
560
585
/// </example>
561
-
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out $quantityName result)
586
+
public static bool TryParse(
587
+
[CanBeNull] string str,
588
+
#if WINDOWS_UWP
589
+
[CanBeNull] string cultureName,
590
+
#else
591
+
[CanBeNull] IFormatProvider provider,
592
+
#endif
593
+
out $quantityName result)
562
594
{
595
+
#if WINDOWS_UWP
596
+
// Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
578
625
/// <example>
579
626
/// Length.ParseUnit("m", new CultureInfo("en-US"));
580
627
/// </example>
@@ -588,11 +635,14 @@ namespace UnitsNet
588
635
/// <summary>
589
636
/// Parse a unit string.
590
637
/// </summary>
638
+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
639
+
/// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="UnitSystem" />'s default culture.</param>
591
640
/// <example>
592
641
/// Length.ParseUnit("m", new CultureInfo("en-US"));
593
642
/// </example>
594
643
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
[Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from other .NET targets.")]
596
646
public static $unitEnumName ParseUnit(string str, [CanBeNull] string cultureName)
597
647
{
598
648
return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName));
@@ -601,6 +651,8 @@ namespace UnitsNet
601
651
/// <summary>
602
652
/// Parse a unit string.
603
653
/// </summary>
654
+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
655
+
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
604
656
/// <example>
605
657
/// Length.ParseUnit("m", new CultureInfo("en-US"));
[Obsolete("This should no longer be used. ToString() uses the unit this quantityvalue was constructed with as default. Pass argument BaseUnit to preserve old behavior.")]
688
+
[Obsolete("This is no longer used since we will instead use the quantity's Unit value as default.")]
637
689
/// <summary>
638
690
/// Set the default unit used by ToString(). Default is $baseUnitSingularName
639
691
/// </summary>
@@ -662,52 +714,99 @@ namespace UnitsNet
662
714
/// Get string representation of value and unit. Using two significant digits after radix.
663
715
/// </summary>
664
716
/// <param name="unit">Unit representation to use.</param>
665
-
/// <param name="culture">Culture to use for localization and number formatting.</param>
717
+
#if WINDOWS_UWP
718
+
/// <param name="cultureName">Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to <see cref="UnitSystem" />'s default culture.</param>
719
+
#else
720
+
/// <param name="provider">Format to use for localization and number formatting. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
721
+
#endif
666
722
/// <returns>String representation.</returns>
667
-
public string ToString($unitEnumName unit, [CanBeNull] Culture culture)
723
+
public string ToString(
724
+
$unitEnumName unit,
725
+
#if WINDOWS_UWP
726
+
[CanBeNull] string cultureName)
727
+
#else
728
+
[CanBeNull] IFormatProvider provider)
729
+
#endif
668
730
{
669
-
return ToString(unit, culture, 2);
731
+
return ToString(
732
+
unit,
733
+
#if WINDOWS_UWP
734
+
cultureName,
735
+
#else
736
+
provider,
737
+
#endif
738
+
2);
670
739
}
671
740
672
741
/// <summary>
673
742
/// Get string representation of value and unit.
674
743
/// </summary>
675
744
/// <param name="unit">Unit representation to use.</param>
676
-
/// <param name="culture">Culture to use for localization and number formatting.</param>
745
+
#if WINDOWS_UWP
746
+
/// <param name="cultureName">Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to <see cref="UnitSystem" />'s default culture.</param>
747
+
#else
748
+
/// <param name="provider">Format to use for localization and number formatting. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
749
+
#endif
677
750
/// <param name="significantDigitsAfterRadix">The number of significant digits after the radix point.</param>
678
751
/// <returns>String representation.</returns>
679
752
[UsedImplicitly]
680
-
public string ToString($unitEnumName unit, [CanBeNull] Culture culture, int significantDigitsAfterRadix)
753
+
public string ToString(
754
+
$unitEnumName unit,
755
+
#if WINDOWS_UWP
756
+
[CanBeNull] string cultureName,
757
+
#else
758
+
[CanBeNull] IFormatProvider provider,
759
+
#endif
760
+
int significantDigitsAfterRadix)
681
761
{
682
762
double value = As(unit);
683
763
string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);
684
-
return ToString(unit, culture, format);
764
+
return ToString(
765
+
unit,
766
+
#if WINDOWS_UWP
767
+
cultureName,
768
+
#else
769
+
provider,
770
+
#endif
771
+
format);
685
772
}
686
773
687
774
/// <summary>
688
775
/// Get string representation of value and unit.
689
776
/// </summary>
690
-
/// <param name="culture">Culture to use for localization and number formatting.</param>
777
+
#if WINDOWS_UWP
778
+
/// <param name="cultureName">Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to <see cref="UnitSystem" />'s default culture.</param>
779
+
#else
780
+
/// <param name="provider">Format to use for localization and number formatting. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
781
+
#endif
691
782
/// <param name="unit">Unit representation to use.</param>
692
783
/// <param name="format">String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively."</param>
693
784
/// <param name="args">Arguments for string format. Value and unit are implictly included as arguments 0 and 1.</param>
694
785
/// <returns>String representation.</returns>
695
786
[UsedImplicitly]
696
-
public string ToString($unitEnumName unit, [CanBeNull] Culture culture, [NotNull] string format,
787
+
public string ToString(
788
+
$unitEnumName unit,
789
+
#if WINDOWS_UWP
790
+
[CanBeNull] string cultureName,
791
+
#else
792
+
[CanBeNull] IFormatProvider provider,
793
+
#endif
794
+
[NotNull] string format,
697
795
[NotNull] params object[] args)
698
796
{
699
797
if (format == null) throw new ArgumentNullException(nameof(format));
700
798
if (args == null) throw new ArgumentNullException(nameof(args));
701
799
702
-
// Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx
// Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx
0 commit comments