-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPriceUtil.cs
1557 lines (1336 loc) · 48.2 KB
/
PriceUtil.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace DigitalPlatform.Text
{
#if REMOVED
public class PriceUtil
{
#region 级联操作函数
public static string Add(string strText1, string strText2)
{
PriceUtil currency = new PriceUtil(strText1);
return currency.Add(strText2).ToString();
}
string _current = ""; // 金额字符串
public PriceUtil()
{
}
public PriceUtil(string strText)
{
this._current = strText;
}
public PriceUtil Set(string strText)
{
this._current = strText;
return this;
}
public PriceUtil Clear()
{
this._current = "";
return this;
}
public PriceUtil Add(string strText)
{
return this.Operator(strText, "+");
}
public PriceUtil Substract(string strText)
{
return this.Operator(strText, "-");
}
public PriceUtil Mutiple(string strText)
{
return this.Operator(strText, "*");
}
public PriceUtil Divide(string strText)
{
return this.Operator(strText, "/");
}
#if NO
public PriceUtil Operator(string strText, string strOperator)
{
int nRet = 0;
string strError = "";
string strResult = "";
if (strOperator == "*" || strOperator == "/")
{
if (string.IsNullOrEmpty(this._current)
|| string.IsNullOrEmpty(strText))
throw new ArgumentException("乘法和除法运算要求两个操作数都不能为空");
string strString = this._current + strOperator + strText;
nRet = SumPrices(strString,
out strResult,
out strError);
if (nRet == -1)
{
strError = strError + " (SumPrices)'" + strString + "'";
throw new Exception(strError);
}
this._current = strResult;
return this;
}
if (string.IsNullOrEmpty(strText))
return this;
List<string> prices = new List<string>();
string s1 = "";
nRet = SumPrices(this._current,
out s1,
out strError);
if (nRet == -1)
{
strError = strError + " (SumPrices)'" + this._current + "'";
throw new Exception(strError);
}
string s2 = "";
if (strOperator == "-")
{
nRet = NegativePrices(strText,
true,
out s2,
out strError);
if (nRet == -1)
{
strError = strError + " (NegativePrices)'" + strText + "'";
throw new Exception(strError);
}
}
else
{
nRet = SumPrices(strText,
out s2,
out strError);
if (nRet == -1)
{
strError = strError + " (SumPrices)'" + strText + "'";
throw new Exception(strError);
}
}
prices.Add(s1);
prices.Add(s2);
nRet = TotalPrice(prices,
out strResult,
out strError);
if (nRet == -1)
{
strError = strError + " (TotalPrices)'" + StringUtil.MakePathList(prices) + "'";
throw new Exception(strError);
}
this._current = strResult;
return this;
}
#endif
public PriceUtil Operator(string strText, string strOperator)
{
int nRet = 0;
string strError = "";
string strResult = "";
if (strOperator == "*" || strOperator == "/")
{
if (string.IsNullOrEmpty(this._current)
|| string.IsNullOrEmpty(strText))
throw new ArgumentException("乘法和除法运算要求两个操作数都不能为空");
string strString = this._current + strOperator + strText;
nRet = SumPrices(strString,
out strResult,
out strError);
if (nRet == -1)
{
strError = strError + " (SumPrices)'" + strString + "'";
throw new Exception(strError);
}
this._current = strResult;
return this;
}
if (string.IsNullOrEmpty(strText))
return this;
string s2 = "";
if (strOperator == "-")
{
nRet = NegativePrices(strText,
true,
out s2,
out strError);
if (nRet == -1)
{
strError = strError + " (NegativePrices)'" + strText + "'";
throw new Exception(strError);
}
}
else
{
#if NO
nRet = SumPrices(strText,
out s2,
out strError);
if (nRet == -1)
{
strError = strError + " (SumPrices)'" + strText + "'";
throw new Exception(strError);
}
#endif
s2 = strText;
}
string s3 = JoinPriceString(this._current, s2);
nRet = SumPrices(s3,
out strResult,
out strError);
if (nRet == -1)
{
strError = strError + " (SumPrices)'" + s3 + "'";
throw new Exception(strError);
}
this._current = strResult;
return this;
}
public override string ToString()
{
return this._current;
}
#endregion
// 判断两个价格字符串是否相等
public static bool IsEqual(string strPrice1, string strPrice2, string strDefaultPrefix = "CNY")
{
if (strPrice1 == strPrice2)
return true;
try
{
CurrencyItem item1 = CurrencyItem.Parse(strPrice1);
CurrencyItem item2 = CurrencyItem.Parse(strPrice2);
if (string.IsNullOrEmpty(item1.Prefix))
item1.Prefix = strDefaultPrefix;
if (string.IsNullOrEmpty(item2.Prefix))
item2.Prefix = strDefaultPrefix;
if (item1.Prefix == item2.Prefix
&& item1.Value == item2.Value
&& item1.Postfix == item2.Postfix)
return true;
return false;
}
catch
{
return false;
}
}
// 计算价格乘积
// 从PrintOrderForm中转移过来
public static int MultiPrice(string strPrice,
int nCopy,
out string strResult,
out string strError)
{
strError = "";
strResult = "";
int nRet = PriceUtil.ParsePriceUnit(strPrice,
out string strPrefix,
out string strValue,
out string strPostfix,
out strError);
if (nRet == -1)
return -1;
decimal value = 0;
try
{
value = Convert.ToDecimal(strValue);
}
catch
{
strError = "数字 '" + strValue + "' 格式不正确";
return -1;
}
value *= (decimal)nCopy;
strResult = strPrefix + value.ToString(CurrencyItem.fmt) + strPostfix;
return 0;
}
// 能够处理乘号或者除号了
public static string GetPurePrice(string strText)
{
string strError = "";
string strLeft = "";
string strRight = "";
string strOperator = "";
// 先处理乘除号
// return:
// -1 出错
// 0 没有发现乘号、除号
// 1 发现乘号或者除号
int nRet = ParseMultipcation(strText,
out strLeft,
out strRight,
out strOperator,
out strError);
if (nRet == -1)
throw new Exception(strError);
if (nRet == 0)
return OldGetPurePrice(strText);
Debug.Assert(nRet == 1, "");
string strMultiper = "";
string strPrice = "";
if (StringUtil.IsDouble(strLeft) == false
&& StringUtil.IsDouble(strRight) == false)
{
strError = "金额字符串格式错误 '" + strText + "'。乘号或除号的两边必须至少有一边是纯数字";
throw new Exception(strError);
}
if (StringUtil.IsDouble(strLeft) == false)
{
strPrice = strLeft;
strMultiper = strRight;
}
else if (StringUtil.IsDouble(strRight) == false)
{
strPrice = strRight;
strMultiper = strLeft;
if (strOperator == "/")
{
strError = "金额字符串 '" + strText + "' 格式错误。除号的右边才能是纯数字";
throw new Exception(strError);
}
}
else
{
// 默认左边是价格,右边是倍率
strPrice = strLeft;
strMultiper = strRight;
}
string strValue = OldGetPurePrice(strPrice);
decimal value = 0;
try
{
value = Convert.ToDecimal(strValue);
}
catch
{
strError = "单个金额字符串 '" + strPrice + "' 中, 数字部分 '" + strValue + "' 格式不正确";
throw new Exception(strError);
}
if (String.IsNullOrEmpty(strOperator) == false)
{
double multiper = 0;
try
{
multiper = Convert.ToDouble(strMultiper);
}
catch
{
strError = "数字 '" + strMultiper + "' 格式不正确";
throw new Exception(strError);
}
if (strOperator == "*")
{
value = (decimal)((double)value * multiper);
}
else
{
Debug.Assert(strOperator == "/", "");
if (multiper == 0)
{
strError = "金额字符串格式错误 '" + strText + "'。除法运算中,除数不能为0";
throw new Exception(strError);
}
value = (decimal)((double)value / multiper);
}
return value.ToString(CurrencyItem.fmt);
}
return strValue;
}
// 从复杂的字符串中,析出纯粹价格数字部分(包括小数点)。
// 2006/11/15 能处理数字前的正负号
public static string OldGetPurePrice(string strPrice)
{
if (String.IsNullOrEmpty(strPrice) == true)
return strPrice;
string strResult = "";
int nSegment = 0; // 0 非数字段 1数字段 2 非数字段
int nPointCount = 0;
bool bNegative = false; // 是否为负数
for (int i = 0; i < strPrice.Length; i++)
{
char ch = strPrice[i];
if ((ch <= '9' && ch >= '0')
|| ch == '.')
{
if (ch == '.')
{
if (nPointCount == 1)
break; // 已经出现过一个小数点了
nPointCount++;
}
if (nSegment == 0)
{
nSegment = 1;
}
}
else
{
if (nSegment == 0)
{
if (ch == '-')
bNegative = true;
}
if (nSegment == 1)
{
nSegment = 2;
break;
}
}
if (nSegment == 1)
strResult += ch;
}
// 如果第一个就是小数点
if (strResult.Length > 0
&& strResult[0] == '.')
{
strResult = "0" + strResult;
}
// 2008/11/15
if (bNegative == true)
return "-" + strResult;
return strResult;
}
// 汇总价格
// 货币单位不同的,互相独立
// 本函数主要用于显示,可以自动处理出错情况 -- 把错误字符串当作结果返回
// return:
// 汇总后的价格字符串,或者报错信息
public static string TotalPrice(List<string> prices)
{
int nRet = TotalPrice(prices,
out string strResult,
out string strError);
if (nRet == -1)
return strError;
return strResult;
}
// 汇总价格
// 货币单位不同的,互相独立
// 本函数还有另外一个版本,是返回List<string>的
// return:
// -1 error
// 0 succeed
public static int TotalPrice(List<string> prices,
out string strTotalPrice,
out string strError)
{
strError = "";
strTotalPrice = "";
List<string> results = null;
int nRet = TotalPrice(prices,
out results,
out strError);
if (nRet == -1)
return -1;
Debug.Assert(results != null, "");
if (results.Count == 0)
return 0;
strTotalPrice = JoinPriceString(results);
return 0;
}
// 把若干价格字符串结合起来
public static string JoinPriceString(List<string> prices)
{
string strResult = "";
for (int i = 0; i < prices.Count; i++)
{
string strPrice = prices[i].Trim();
if (String.IsNullOrEmpty(strPrice) == true)
continue;
if (strPrice[0] == '+' || strPrice[0] == '-')
strResult += strPrice;
else
{
// 第一个价格前面不用加+号
if (String.IsNullOrEmpty(strResult) == false)
strResult += "+";
strResult += strPrice;
}
}
return strResult;
}
// 连接两个价格字符串
public static string JoinPriceString(string strPrice1,
string strPrice2)
{
if (string.IsNullOrEmpty(strPrice1) == true
&& string.IsNullOrEmpty(strPrice2) == true)
return "";
if (string.IsNullOrEmpty(strPrice1) == true)
return strPrice2;
if (string.IsNullOrEmpty(strPrice2) == true)
return strPrice1;
strPrice1 = strPrice1.Trim();
strPrice2 = strPrice2.Trim();
if (String.IsNullOrEmpty(strPrice1) == true
&& String.IsNullOrEmpty(strPrice2) == true)
return "";
if (String.IsNullOrEmpty(strPrice1) == true)
return strPrice2;
if (String.IsNullOrEmpty(strPrice2) == true)
return strPrice1;
if (strPrice2[0] == '+'
|| strPrice2[0] == '-')
return strPrice1 + strPrice2;
return strPrice1 + "+" + strPrice2;
}
// 将形如"-123.4+10.55-20.3"的价格字符串反转正负号
// parameters:
// bSum 是否要顺便汇总? true表示要汇总
public static int NegativePrices(string strPrices,
bool bSum,
out string strResultPrice,
out string strError)
{
strError = "";
strResultPrice = "";
strPrices = strPrices.Trim();
if (String.IsNullOrEmpty(strPrices) == true)
return 0;
List<string> prices = null;
// 将形如"-123.4+10.55-20.3"的价格字符串切割为单个的价格字符串,并各自带上正负号
// return:
// -1 error
// 0 succeed
int nRet = SplitPrices(strPrices,
out prices,
out strError);
if (nRet == -1)
return -1;
// 直接每个反转
if (bSum == false)
{
for (int i = 0; i < prices.Count; i++)
{
string strOnePrice = prices[i];
if (String.IsNullOrEmpty(strOnePrice) == true)
continue;
if (strOnePrice[0] == '+')
strResultPrice += "-" + strOnePrice.Substring(1);
else if (strOnePrice[0] == '-')
strResultPrice += "+" + strOnePrice.Substring(1);
else
strResultPrice += "-" + strOnePrice; // 缺省为正数
}
return 0;
}
List<string> results = new List<string>();
// 汇总价格
// 货币单位不同的,互相独立
// return:
// -1 error
// 0 succeed
nRet = TotalPrice(prices,
out results,
out strError);
if (nRet == -1)
return -1;
for (int i = 0; i < results.Count; i++)
{
string strOnePrice = results[i];
if (String.IsNullOrEmpty(strOnePrice) == true)
continue;
if (strOnePrice[0] == '+')
strResultPrice += "-" + strOnePrice.Substring(1);
else if (strOnePrice[0] == '-')
strResultPrice += "+" + strOnePrice.Substring(1);
else
strResultPrice += "-" + strOnePrice; // 缺省为正数
}
return 0;
}
// 比较两个价格字符串
// return:
// -3 币种不同,无法直接比较 strError中有说明
// -2 error strError中有说明
// -1 strPrice1小于strPrice2
// 0 等于
// 1 strPrice1大于strPrice2
public static int Compare(string strPrice1,
string strPrice2,
out string strError)
{
strError = "";
string strPrefix1 = "";
string strValue1 = "";
string strPostfix1 = "";
int nRet = ParsePriceUnit(strPrice1,
out strPrefix1,
out strValue1,
out strPostfix1,
out strError);
if (nRet == -1)
{
strError = "金额字符串1 '" + strPrice1 + "' 解析出错: " + strError;
return -2;
}
decimal value1 = 0;
try
{
value1 = Convert.ToDecimal(strValue1);
}
catch
{
strError = "数字 '" + strValue1 + "' 格式不正确";
return -2;
}
if (strPrefix1 == "" && strPostfix1 == "")
strPrefix1 = "CNY";
string strPrefix2 = "";
string strValue2 = "";
string strPostfix2 = "";
nRet = ParsePriceUnit(strPrice2,
out strPrefix2,
out strValue2,
out strPostfix2,
out strError);
if (nRet == -1)
{
strError = "金额字符串2 '" + strPrice2 + "' 解析出错: " + strError;
return -2;
}
if (strPrefix2 == "" && strPostfix2 == "")
strPrefix2 = "CNY";
if (strPrefix1 != strPrefix2
|| strPostfix1 != strPostfix2)
{
strError = "币种不同(一个是'" + strPrice1 + "',一个是'" + strPrice2 + "'),无法进行金额比较";
return -3;
}
decimal value2 = 0;
try
{
value2 = Convert.ToDecimal(strValue2);
}
catch
{
strError = "数字 '" + strValue2 + "' 格式不正确";
return -2;
}
if (value1 < value2)
return -1;
if (value1 == value2)
return 0;
Debug.Assert(value1 > value2, "");
return 1;
}
// 看看若干个价格字符串是否都表示了0?
// return:
// -1 出错
// 0 不为0
// 1 为0
public static int IsZero(List<string> prices,
out string strError)
{
strError = "";
List<CurrencyItem> items = new List<CurrencyItem>();
// 变换为PriceItem
for (int i = 0; i < prices.Count; i++)
{
string strPrefix = "";
string strValue = "";
string strPostfix = "";
int nRet = ParsePriceUnit(prices[i],
out strPrefix,
out strValue,
out strPostfix,
out strError);
if (nRet == -1)
return -1;
decimal value = 0;
try
{
value = Convert.ToDecimal(strValue);
}
catch
{
strError = "数字 '" + strValue + "' 格式不正确";
return -1;
}
CurrencyItem item = new CurrencyItem();
item.Prefix = strPrefix;
item.Postfix = strPostfix;
item.Value = value;
items.Add(item);
}
// 分析
for (int i = 0; i < items.Count; i++)
{
CurrencyItem item = items[i];
if (item.Value != 0)
return 0; // 中间出现了不为0的
}
return 1; // 全部为0
}
// 将形如"-123.4+10.55-20.3"的价格字符串归并汇总
public static int SumPrices(string strPrices,
out List<string> results,
out string strError)
{
strError = "";
results = new List<string>();
List<string> prices = null;
// 将形如"-123.4+10.55-20.3"的价格字符串切割为单个的价格字符串,并各自带上正负号
// return:
// -1 error
// 0 succeed
int nRet = SplitPrices(strPrices,
out prices,
out strError);
if (nRet == -1)
return -1;
// 汇总价格
// 货币单位不同的,互相独立
// return:
// -1 error
// 0 succeed
nRet = TotalPrice(prices,
out results,
out strError);
if (nRet == -1)
return -1;
return 0;
}
// 2012/3/7
// 将形如"-123.4+10.55-20.3"的价格字符串归并汇总
public static int SumPrices(string strPrices,
out string strSumPrices,
out string strError)
{
strError = "";
strSumPrices = "";
List<string> prices = null;
// 将形如"-123.4+10.55-20.3"的价格字符串切割为单个的价格字符串,并各自带上正负号
// return:
// -1 error
// 0 succeed
int nRet = SplitPrices(strPrices,
out prices,
out strError);
if (nRet == -1)
return -1;
List<string> results = new List<string>();
// 汇总价格
// 货币单位不同的,互相独立
// return:
// -1 error
// 0 succeed
nRet = TotalPrice(prices,
out results,
out strError);
if (nRet == -1)
return -1;
strSumPrices = JoinPriceString(results);
return 0;
}
// 将形如"-123.4+10.55-20.3"的价格字符串切割为单个的价格字符串,并各自带上正负号
// return:
// -1 error
// 0 succeed
public static int SplitPrices(string strPrices,
out List<string> prices,
out string strError)
{
strError = "";
prices = new List<string>();
if (strPrices == null)
strPrices = "";
// 2018/2/6
// 这里假定字符 ` 在数据中不常见。或者可以考虑使用一个更不常见的字符
strPrices = strPrices.Replace("+", "`+").Replace("-", "`-");
string[] parts = strPrices.Split(new char[] { '`' });
for (int i = 0; i < parts.Length; i++)
{
string strPart = parts[i].Trim();
if (String.IsNullOrEmpty(strPart) == true)
continue;
prices.Add(strPart);
}
return 0;
}
// 2012/3/7
// 校验金额字符串格式正确性
// return:
// -1 有错
// 0 没有错
public static int VerifyPriceFormat(
List<string> valid_formats,
string strString,
out string strError)
{
strError = "";
// 没有格式定义,就不作校验
if (valid_formats.Count == 0)
return 0;
string strPrefix = "";
string strValue = "";
string strPostfix = "";
int nRet = ParsePriceUnit(strString,
out strPrefix,
out strValue,
out strPostfix,
out strError);
if (nRet == -1)
return -1;
foreach (string fmt in valid_formats)
{
string[] parts = fmt.Split(new char[] { '|' });
string strPrefixFormat = "";
string strValueFormat = "";
string strPostfixFormat = "";
if (parts.Length > 0)
strPrefixFormat = parts[0];
if (parts.Length > 1)
strValueFormat = parts[1];
if (parts.Length > 2)
strPostfixFormat = parts[2];
if (string.IsNullOrEmpty(strPrefixFormat) == false
&& strPrefix != strPrefixFormat)
continue;
// 暂时不校验value部分
if (string.IsNullOrEmpty(strPostfixFormat) == false
&& strPostfix != strPostfixFormat)
continue;
return 0;
}
strError = "金额字符串 '" + strString + "' 的格式不符合定义 '" + StringUtil.MakePathList(valid_formats) + "' 的要求";
return -1;
}
// 分析价格参数
// 允许前面出现+ -号
// return:
// -1 出错
// 0 成功
public static int ParsePriceUnit(string strString,
out string strPrefix,
out string strValue,
out string strPostfix,
out string strError)
{
strPrefix = "";
strValue = "";
strPostfix = "";
strError = "";
strString = strString.Trim();
// 去掉逗号 2012/9/1
strString = strString.Replace(",", "");
strString = strString.Replace(",", "");
if (String.IsNullOrEmpty(strString) == true)
{
strError = "金额字符串为空";
return -1;
}
bool bNegative = false; // 是否为负数
if (strString[0] == '+')
{
bNegative = false;
strString = strString.Substring(1).Trim();
}
else if (strString[0] == '-')
{
bNegative = true;
strString = strString.Substring(1).Trim();
}
if (String.IsNullOrEmpty(strString) == true)
{
strError = "金额字符串(除了正负号以外)为空";
return -1;
}
bool bInPrefix = true;
int nDotCount = 0;
for (int i = 0; i < strString.Length; i++)
{
char ch = strString[i];
if ((ch >= '0' && ch <= '9')
|| ch == '.')
{
bInPrefix = false;
strValue += ch;
if (ch == '.') // 2017/9/15
nDotCount++;
}
else
{
if (bInPrefix == true)
strPrefix += ch;
else
{
strPostfix = strString.Substring(i).Trim();
break;
}
}