-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAdjustDigits.cs
190 lines (186 loc) · 9.24 KB
/
AdjustDigits.cs
1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdjustDigits
{
class Program
{
static void Main(string[] args)
{
//目标为控制位数在10位
string Number;
Console.WriteLine("请输入数字");
Number = Console.ReadLine();
Number = Number.Trim();
int CountDecimalPoint = 0;
int ScientificNumber;
string ScientificNotation = "";
int digits_part;
BigNumber NumberProcess = new BigNumber("0");
string ProcessedNumber;
char[] separator = { '.' };
//用于分割小数点
string[] NumberParts = new string[2];
int IsNegative = 0;
//用来判定数字是否为负
int zero_count = 0;
//用于计算小数点后有多少个零
NumberParts = Number.Split(separator);
if (NumberParts[0].Length + 5 > 10)
//四位小数加上1个小数点
{
if (Number[0] == '-'){
IsNegative = 1;
}
foreach (char FindDecimalPoint in Number)
{
if (FindDecimalPoint == '.')
{
CountDecimalPoint = 1;
break;
}
}
if (CountDecimalPoint == 0)
{
//无小数点则只有整数部分
ScientificNumber = Number.Length - IsNegative - 1;
//转换为科学计数法
//Console.WriteLine("ScientificNumber = {0}", ScientificNumber);
if (ScientificNumber <= 9 && ScientificNumber > 0)
{
ScientificNotation = "0" + ScientificNumber.ToString();
//如果科学计数法为个位要补零
}
else
{
ScientificNotation = ScientificNumber.ToString();
}
//数字正负无需考虑
if (ScientificNotation.Length + 1 + 2 <= 9)
{
//例如:-3.1415E+05
//数字负号占一位,E和科学计数法的符号占两位
//大于9的情况不做考虑,数字太大。
digits_part = 9 - (ScientificNotation.Length + 1 + 2);
//小数位由此算出
NumberProcess = new BigNumber (Number.ToString()) / (new BigNumber("10")).Power(new BigNumber(ScientificNumber.ToString()),30);
//Console.WriteLine("ScientificNotation = {0},digits_part = {1},NumberProcess = {2}", ScientificNotation, digits_part, NumberProcess.ToString());
ProcessedNumber = MathV.round(NumberProcess.ToString(), digits_part, 0) + "E+"+ScientificNotation ;
//Console.WriteLine("ProcessedNumber ={0}", ProcessedNumber);
}
else
{
//如果已经大于已经大于九位了,则不作处理
//这种情况极为罕见,不考虑
NumberProcess = new BigNumber(Number.ToString()) / (new BigNumber("10")).Power(new BigNumber(ScientificNumber.ToString()), 30);
ProcessedNumber = MathV.round(NumberProcess.ToString(), 0, 0) + "E+" + ScientificNotation;
}
}
else
{
//有小数点的情况
NumberParts = Number.Split(separator);
if (NumberParts[0] != "0")
{
if (NumberParts[0].Length + 1 + 4 >= 9)
{
//如果保留四位小数之后依旧总长度超过九,则进行如下操作
//判断整数部分是否为0
//如果不为0,继续科学计数法的处理,舍去小数部分
ScientificNumber = NumberParts[0].Length - IsNegative - 1;
//转换为科学计数法
if (ScientificNumber <= 9 && ScientificNumber > 0)
{
ScientificNotation = "0" + ScientificNumber.ToString();
//如果科学计数法为个位要补零
}
else
{
ScientificNotation = ScientificNumber.ToString();
}
//数字正负无需考虑
if (ScientificNotation.Length + 1 + 2 <= 9)
{
//例如:-3.1415E+05
//数字负号占一位,E和科学计数法的符号占两位
//大于9的情况不做考虑,数字太大。
digits_part = 9 - (ScientificNotation.Length + 1 + 2);
//小数位由此算出
NumberProcess = new BigNumber(Number.ToString()) / (new BigNumber("10")).Power(new BigNumber(ScientificNumber.ToString()), 30);
ProcessedNumber = MathV.round(NumberProcess.ToString(), digits_part, 0) + "E+" + ScientificNotation;
}
else
{
//如果已经大于已经大于九位了,则不作处理
//这种情况极为罕见,不考虑
NumberProcess = new BigNumber(Number.ToString()) / (new BigNumber("10")).Power(new BigNumber(ScientificNumber.ToString()), 30);
ProcessedNumber = MathV.round(NumberProcess.ToString(), 0, 0) + "E+" + ScientificNotation;
}
}
else
{
ProcessedNumber = MathV.round(Number, 4, 0);
}
}
else
{
zero_count = 0;
foreach (char EachDigit in NumberParts[1])
{
if (EachDigit == '0')
{
zero_count++;
}
else
{
break;
}
}
ScientificNumber = zero_count + 1;
//科学计数法的数字为零的个数加1
if (ScientificNumber <= 9 && ScientificNumber > 0)
{
ScientificNotation = "0" + ScientificNumber.ToString();
//如果科学计数法为个位要补零
}
else
{
ScientificNotation = ScientificNumber.ToString();
}
if (ScientificNotation.Length + 1 + 2 <= 9)
{
//例如:-2.654E-05
//数字负号占一位,E和科学计数法的符号占两位
//大于9的情况不做考虑,数字太大。
digits_part = 9 - (ScientificNotation.Length + 1 + 2);
//小数位由此算出
NumberProcess = new BigNumber(Number.ToString()) * (new BigNumber("10")).Power(new BigNumber(ScientificNumber.ToString()), 30);
ProcessedNumber = MathV.round(NumberProcess.ToString(), digits_part, 0) + "E-" + ScientificNotation;
}
else
{
//如果已经大于已经大于九位了,则不作处理
//这种情况极为罕见,不考虑
NumberProcess = new BigNumber(Number.ToString()) * (new BigNumber("10")).Power(new BigNumber(ScientificNumber.ToString()), 30);
ProcessedNumber = MathV.round(NumberProcess.ToString(), 0, 0) + "E-" + ScientificNotation;
}
}
}
}
else {
ProcessedNumber = MathV.round(Number,4,0);
//如果本身长度就在十个单位以内,则不作处理
//保留四位小数
}
if (ProcessedNumber.Length < 11)
{
ProcessedNumber = ProcessedNumber.PadLeft(11, ' ');
//11个单位的长度是为了预留量
}
Console.WriteLine("ProcessedNumber={0}", ProcessedNumber);
Console.ReadKey();
}
}
}