|
| 1 | +function Get-YamlNormalizedFloat { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Normalizes a finite CLR floating-point value to a decimal significand and exponent. |
| 5 | + #> |
| 6 | + [CmdletBinding()] |
| 7 | + [OutputType([string])] |
| 8 | + param ( |
| 9 | + [Parameter(Mandatory)] |
| 10 | + [object] $Value |
| 11 | + ) |
| 12 | + |
| 13 | + $culture = [System.Globalization.CultureInfo]::InvariantCulture |
| 14 | + if ($Value -is [decimal]) { |
| 15 | + if ($Value -eq 0) { |
| 16 | + return '0e0' |
| 17 | + } |
| 18 | + $text = $Value.ToString('G29', $culture) |
| 19 | + } elseif ($Value -is [single]) { |
| 20 | + $number = [single] $Value |
| 21 | + if ([single]::IsNaN($number)) { |
| 22 | + return 'nan' |
| 23 | + } |
| 24 | + if ([single]::IsPositiveInfinity($number)) { |
| 25 | + return '+inf' |
| 26 | + } |
| 27 | + if ([single]::IsNegativeInfinity($number)) { |
| 28 | + return '-inf' |
| 29 | + } |
| 30 | + if ($number -eq 0) { |
| 31 | + return '0e0' |
| 32 | + } |
| 33 | + $text = $number.ToString('R', $culture) |
| 34 | + } elseif ($Value -is [double]) { |
| 35 | + $number = [double] $Value |
| 36 | + if ([double]::IsNaN($number)) { |
| 37 | + return 'nan' |
| 38 | + } |
| 39 | + if ([double]::IsPositiveInfinity($number)) { |
| 40 | + return '+inf' |
| 41 | + } |
| 42 | + if ([double]::IsNegativeInfinity($number)) { |
| 43 | + return '-inf' |
| 44 | + } |
| 45 | + if ($number -eq 0) { |
| 46 | + return '0e0' |
| 47 | + } |
| 48 | + $text = $number.ToString('R', $culture) |
| 49 | + } else { |
| 50 | + throw [System.ArgumentException]::new( |
| 51 | + "Type '$($Value.GetType().FullName)' is not a supported floating-point type.", |
| 52 | + 'Value' |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + $match = [regex]::Match( |
| 57 | + $text, |
| 58 | + '^(?<sign>[-+]?)(?<integer>[0-9]+)(?:\.(?<fraction>[0-9]*))?(?:[eE](?<exponent>[-+]?[0-9]+))?$', |
| 59 | + [System.Text.RegularExpressions.RegexOptions]::CultureInvariant |
| 60 | + ) |
| 61 | + if (-not $match.Success) { |
| 62 | + throw [System.InvalidOperationException]::new( |
| 63 | + "The invariant floating-point text '$text' could not be normalized." |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + $fraction = $match.Groups['fraction'].Value |
| 68 | + $digits = ($match.Groups['integer'].Value + $fraction).TrimStart('0') |
| 69 | + if ($digits.Length -eq 0) { |
| 70 | + return '0e0' |
| 71 | + } |
| 72 | + |
| 73 | + $exponent = 0 - $fraction.Length |
| 74 | + if ($match.Groups['exponent'].Success) { |
| 75 | + $exponent += [int]::Parse( |
| 76 | + $match.Groups['exponent'].Value, |
| 77 | + [System.Globalization.NumberStyles]::AllowLeadingSign, |
| 78 | + $culture |
| 79 | + ) |
| 80 | + } |
| 81 | + while ($digits.Length -gt 1 -and $digits[$digits.Length - 1].Equals([char] '0')) { |
| 82 | + $digits = $digits.Substring(0, $digits.Length - 1) |
| 83 | + $exponent++ |
| 84 | + } |
| 85 | + |
| 86 | + $sign = if ($match.Groups['sign'].Value.Equals( |
| 87 | + '-', |
| 88 | + [System.StringComparison]::Ordinal |
| 89 | + )) { |
| 90 | + '-' |
| 91 | + } else { |
| 92 | + '' |
| 93 | + } |
| 94 | + return '{0}{1}e{2}' -f $sign, $digits, $exponent |
| 95 | +} |
0 commit comments