1 |
#Region "Microsoft.VisualBasic::1df4e3735eeeb7d27c92829b1bbef491, Microsoft.VisualBasic.Core\Extensions\StringHelpers\NumericFormatHelper.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
#End Region
|
41 |
|
42 |
Imports System.Runtime.CompilerServices
|
43 |
Imports Microsoft.VisualBasic.Language
|
44 |
|
45 |
|
46 |
|
47 |
'''
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
'''
|
53 |
|
54 |
'''
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
###### ``E``科学型
|
61 |
'''
|
62 |
|
63 |
25000.ToString("E")
|
64 |
|
65 |
|
66 |
|
67 |
###### ``F``固定点
|
68 |
'''
|
69 |
|
70 |
25.ToString("F2")
|
71 |
|
72 |
|
73 |
|
74 |
###### ``G``常规
|
75 |
'''
|
76 |
|
77 |
2.5.ToString("G")
|
78 |
|
79 |
|
80 |
'''
|
81 |
###### ``N``数字
|
82 |
'''
|
83 |
|
84 |
2500000.ToString("N")
|
85 |
|
86 |
|
87 |
|
88 |
###### ``X``十六进制
|
89 |
'''
|
90 |
|
91 |
255.ToString("X")
|
92 |
|
93 |
|
94 |
</summary>
|
95 |
Public Module NumericFormatHelper
|
96 |
|
97 |
|
98 |
``D<n>``
|
99 |
</summary>
|
100 |
<param name="n%"></param>
|
101 |
<returns></returns>
|
102 |
Public Function [Decimal](n%) As String
|
103 |
Return "D" & n
|
104 |
End Function
|
105 |
|
106 |
|
107 |
``F<n>``
|
108 |
</summary>
|
109 |
<param name="n%"></param>
|
110 |
<returns></returns>
|
111 |
Public Function Float(n%) As String
|
112 |
Return "F" & n
|
113 |
End Function
|
114 |
|
115 |
|
116 |
|
117 |
</summary>
|
118 |
<param name="x#"></param>
|
119 |
<param name="NaN_imaginary$">Default using R language style default numeric value</param>
|
120 |
<returns></returns>
|
121 |
<Extension>
|
122 |
Public Function SafeToString(x#, Optional NaN_imaginary$ = "NA") As String
|
123 |
If x.IsNaNImaginary Then
|
124 |
Return NaN_imaginary
|
125 |
Else
|
126 |
Return CStr(x)
|
127 |
End If
|
128 |
End Function
|
129 |
|
130 |
<Extension>
|
131 |
Public Function SafeToStrings(v As IEnumerable(Of Double), Optional NaN_imaginary$ = "NA") As String()
|
132 |
Return v _
|
133 |
.Select(Function(x) x.SafeToString(NaN_imaginary)) _
|
134 |
.ToArray
|
135 |
End Function
|
136 |
|
137 |
#Region "PrimitiveExtensions"
|
138 |
|
139 |
<Extension>
|
140 |
Public Function ToHexString(this As Byte) As String
|
141 |
Return this.ToString("x2")
|
142 |
End Function
|
143 |
|
144 |
<Extension>
|
145 |
Public Function ToHexString(this As Short) As String
|
146 |
Dim value As UShort = CUShort(this)
|
147 |
Return ToHexString(value)
|
148 |
End Function
|
149 |
|
150 |
<Extension>
|
151 |
Public Function ToHexString(this As UShort) As String
|
152 |
Dim reverse As UShort = CUShort(((&HFF00 And this) >> 8) Or ((&HFF And this) << 8))
|
153 |
Return reverse.ToString("x4")
|
154 |
End Function
|
155 |
|
156 |
<Extension>
|
157 |
Public Function ToHexString(this As Integer) As String
|
158 |
Dim value As UInteger = CUInt(this)
|
159 |
Return ToHexString(value)
|
160 |
End Function
|
161 |
|
162 |
<Extension>
|
163 |
Public Function ToHexString(this As UInteger) As String
|
164 |
Dim reverse As UInteger = ((&HFF000000UI And this) >> 24) Or ((&HFF0000 And this) >> 8) Or ((&HFF00 And this) << 8) Or ((&HFF And this) << 24)
|
165 |
Return reverse.ToString("x8")
|
166 |
End Function
|
167 |
|
168 |
<Extension>
|
169 |
Public Function ToHexString(this As Long) As String
|
170 |
Dim value As ULong = CULng(this)
|
171 |
Return ToHexString(value)
|
172 |
End Function
|
173 |
|
174 |
<Extension>
|
175 |
Public Function ToHexString(this As ULong) As String
|
176 |
Dim reverse As ULong = (this And &HFFUL) << 56 Or (this And &HFF00UL) << 40 Or (this And &HFF0000UL) << 24 Or (this And &HFF000000UL) << 8 Or (this And &HFF00000000UL) >> 8 Or (this And &HFF0000000000UL) >> 24 Or (this And &HFF000000000000UL) >> 40 Or (this And &HFF00000000000000UL) >> 56
|
177 |
Return reverse.ToString("x16")
|
178 |
End Function
|
179 |
|
180 |
<Extension>
|
181 |
Public Function ToHexString(this As Single) As String
|
182 |
Dim value As UInteger = Numeric.ToUInt32(this)
|
183 |
Return ToHexString(value)
|
184 |
End Function
|
185 |
|
186 |
<Extension>
|
187 |
Public Function ToHexString(this As Double) As String
|
188 |
Dim value As ULong = Numeric.ToUInt64(this)
|
189 |
Return ToHexString(value)
|
190 |
End Function
|
191 |
#End Region
|
192 |
End Module
|