| 1 | #Region "Microsoft.VisualBasic::2998ab66ceb084246f131aefc80fd130, Microsoft.VisualBasic.Core\Extensions\Math\ScientificNotation.vb" | 
| 2 | |
| 3 | ' Author: | 
| 4 | ' | 
| 5 | '       asuka (amethyst.asuka@gcmodeller.org) | 
| 6 | '       xie (genetics@smrucc.org) | 
| 7 | '       xieguigang (xie.guigang@live.com) | 
| 8 | ' | 
| 9 | ' Copyright (c) 2018 GPL3 Licensed | 
| 10 | ' | 
| 11 | ' | 
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) | 
| 13 | ' | 
| 14 | ' | 
| 15 | ' This program is free software: you can redistribute it and/or modify | 
| 16 | ' it under the terms of the GNU General Public License as published by | 
| 17 | ' the Free Software Foundation, either version 3 of the License, or | 
| 18 | ' (at your option) any later version. | 
| 19 | ' | 
| 20 | ' This program is distributed in the hope that it will be useful, | 
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 23 | ' GNU General Public License for more details. | 
| 24 | ' | 
| 25 | ' You should have received a copy of the GNU General Public License | 
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. | 
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ | 
| 31 | |
| 32 | ' Summaries: | 
| 33 | |
| 34 | '     Module ScientificNotation | 
| 35 | ' | 
| 36 | '         Function: FormatScientificNotation, PowerLog10, UsingScientificNotation | 
| 37 | ' | 
| 38 | ' | 
| 39 | ' /********************************************************************************/ | 
| 40 | |
| 41 | #End Region | 
| 42 | |
| 43 | Imports System.Runtime.CompilerServices | 
| 44 | Imports System.Text.RegularExpressions | 
| 45 | Imports sys = System.Math | 
| 46 | |
| 47 | Namespace Math | 
| 48 | |
| 49 | ''' <summary> | 
| 50 | ''' 科学记数法 | 
| 51 | ''' </summary> | 
| 52 | Public Module ScientificNotation | 
| 53 | |
| 54 | ''' <summary> | 
| 55 | ''' 返回零表示比较小的常数 | 
| 56 | ''' </summary> | 
| 57 | ''' <param name="x"></param> | 
| 58 | ''' <param name="INF">当位数超过这个值之后将会被判定为非常大或者非常小的一个数</param> | 
| 59 | ''' <returns></returns> | 
| 60 | Public Function PowerLog10(x#, Optional INF% = 5) As Single | 
| 61 | Dim pow# = sys.Log10(sys.Abs(x)) | 
| 62 | |
| 63 | If pow < -INF Then | 
| 64 | Return pow | 
| 65 | End If | 
| 66 | If pow > INF Then | 
| 67 | Return pow | 
| 68 | End If | 
| 69 | |
| 70 | Return 0 | 
| 71 | End Function | 
| 72 | |
| 73 | ''' <summary> | 
| 74 | ''' 是否需要科学记数法来格式化? | 
| 75 | ''' </summary> | 
| 76 | ''' <param name="x#"></param> | 
| 77 | ''' <param name="INF%"></param> | 
| 78 | ''' <returns></returns> | 
| 79 | Public Function UsingScientificNotation(x#, Optional INF% = 5) As Boolean | 
| 80 | Return ScientificNotation.PowerLog10(x, INF) <> 0! | 
| 81 | End Function | 
| 82 | |
| 83 | ''' <summary> | 
| 84 | ''' 强制格式化为科学记数法 | 
| 85 | ''' </summary> | 
| 86 | ''' <param name="decimal%"></param> | 
| 87 | ''' <returns></returns> | 
| 88 | Public Function FormatScientificNotation(n#, decimal%) As String | 
| 89 | If n# = 0R Then | 
| 90 | Return "0" | 
| 91 | End If | 
| 92 | |
| 93 | Dim power = Fix(sys.Log10(n)) | 
| 94 | Dim s = n.ToString.Split("E"c, "e"c).First | 
| 95 | Dim t$() = s.Split("."c) | 
| 96 | |
| 97 | ' 处理整数部分 | 
| 98 | Dim int As String = t(Scan0) | 
| 99 | Dim intpower = int.Length - 1 | 
| 100 | |
| 101 | If intpower > 0 Then  ' 整数部分不止一个字符长度,即数位大于等于2 | 
| 102 | int = int.First & "." & Mid(int, 1) & t(1) | 
| 103 | s = sys.Round(Val(int), [decimal]) | 
| 104 | Else | 
| 105 | s = int & "." & t(1) | 
| 106 | s = sys.Round(Val(s), [decimal]) | 
| 107 | End If | 
| 108 | |
| 109 | s = s & $"E{power + intpower}" | 
| 110 | |
| 111 | Return s | 
| 112 | End Function | 
| 113 | End Module | 
| 114 | End Namespace |