//把整形 转换成(缩小指定倍数) 带小数点的 字符串. public string L2A ( long l , int PointW ) { string str , str1 , str2 , strRet = ""; try { str = String.Format ( "{0}" , l ); int len = str.Length; int n = len - PointW ; if ( n < 0 ) { string strZreo = "0." ; for ( int i = 0 ; i < PointW - len ; i++ ) strZreo += "0"; strRet = string.Format ( "{0}{1}" , strZreo , l ); } else if ( n == 0 ) { strRet = string.Format ( "0.{0}" , l ); } else { str1 = str.Substring ( 0 , len - PointW ); //get left part str2 = str.Substring ( str.Length - PointW , PointW ); //get right part strRet = string.Format ( "{0}.{1}" , str1 , str2 ); //.Format ( "%s.%s" ,str1 , str2 ) ; } } catch ( Exception e ) { FrontFlag.CONTROL ctrl = new CONTROL (); ctrl.MsgBox.Show ( "Err at L2A() function" ); } int m = strRet.IndexOf ( '.' ); //让人看起来 是精确到分。 if ( m < 0 ) { strRet += ".00"; } else { if ( m == strRet.Length -1 ) //1234. 的 情况。 strRet += "00"; else if ( m == strRet.Length - 2 ) //1234.5 的 情况。 strRet += "0"; } return strRet; }
//用于处理 带小数的字符串 转换成长整形. //扩大指定的倍数, 如果小数点不足, 会在转换后的字符串后面补零. public long A2LFix ( string str , int PointW ) { if ( str.Trim () == String.Empty ) return 0; long lRet = 0; try { int n = str.IndexOf ( "." ); //是整数,直接扩大倍数. if ( n < 0 ) { lRet = Convert.ToInt32 ( str ) ; for ( int i = 0 ; i < PointW ; i ++ ) lRet *= 10 ; } else { int len = str.Length ; int curPW = len - 1 - n ; //目前的小数点位数 string str1 , str2 ; str1 = str.Substring ( 0 , n ) ; //get left part of pint. str2 = str.Substring( str.Length - curPW , curPW ) ; //get right part of pint. str = str1 + str2 ; //combin without point. lRet = Convert.ToInt32 ( str ) ; if ( curPW < PointW ) //指定的倍数大于目前的倍数, 扩大差额的部分。 { for ( int i = 0 ; i < PointW-curPW ; i ++ ) lRet *= 10 ; } else if ( curPW > PointW ) //指定的倍数小于目前的倍数, 缩小差额的部分。会丢失部分精度. { for ( int i = 0 ; i < curPW-PointW ; i ++ ) lRet /= 10 ; } } } catch ( Exception e ) { FrontFlag.CONTROL ctrl = new CONTROL (); ctrl.MsgBox.Show ( "Err at A2LFix() function" ); } return lRet ; }
//把放大处理后的整形(MACRO_PricePoint倍数精度)的钱币数值, 转换成带小数点(.角分厘)的字符串。 public string Price2Str ( long lPrice ) { int PricePointW = MACRO_PricePoint; string str , str1 , str2 , strRet = ""; try { str = String.Format ( "{0}" , lPrice ); int len = str.Length; int n = len - PricePointW; if ( n < 0 ) { string strZreo = "0."; for ( int i = 0 ; i < PricePointW - len ; i++ ) strZreo += "0"; strRet = string.Format ( "{0}{1}" , strZreo , lPrice ); } else if ( n == 0 ) { strRet = String.Format ( "0.{0}" , lPrice ); } else { str1 = str.Substring ( 0 , len - PricePointW ); //get left str2 = str.Substring ( str.Length - PricePointW , PricePointW ); //get right strRet = String.Format ( "{0}.{1}" , str1 , str2 ); } } catch ( Exception e ) { FrontFlag.CONTROL ctrl = new CONTROL (); ctrl.MsgBox.Show ( "Err at Price2Str() function" ); } return strRet; }
//用于处理 带小数的字符串 转换成长整形. //自动扩大倍数, 直到小数点消失为止, 最终的扩大倍数通过 PointW 返回. public long A2L ( string str , ref int PointW ) { if ( str.Trim () == String.Empty ) return 0; PointW = 0; long l = 0; try { int n = str.IndexOf ( "." ); if ( n < 0 ) { l = Convert.ToInt32 ( str ); } else { int len = str.Length; PointW = len - 1 - n; //得到小数点位置, 转换后的字符串,即是按这个位置的放放大倍数的整数。 string str1 , str2; str1 = str.Substring ( 0 , n ); //get left part of pint. str2 = str.Substring ( str.Length - PointW , PointW ); //get right part of pint. str = str1 + str2; //combin without point. l = Convert.ToInt32 ( str ); } } catch ( Exception e ) { FrontFlag.CONTROL ctrl = new CONTROL (); ctrl.MsgBox.Show ( "Err at A2L() function" ); } return l; }
//转换后,钱币数字同时放大 MACRO_PricePoint 倍,保证是一个没有小数点的 long性数字。 public long Str2Price ( string str ) { if ( str.Trim () == String.Empty ) return 0; int PricePointW = MACRO_PricePoint; long lPrice = 0; int PointW = 0; try { int n = str.IndexOf ( "." ); //F.ind ( "." ) ; if ( n < 0 ) { lPrice = Convert.ToInt32 ( str ); //atol ( str ) ; for ( int i = 0 ; i < PricePointW ; i++ ) lPrice *= 10; } else { int len = str.Length; PointW = len - 1 - n; string str1 , str2; str1 = str.Substring ( 0 , n ); //str.Left ( n ) ; str2 = str.Substring ( str.Length - PointW , PointW ); //str.Right ( PointW ) ; str = str1 + str2; lPrice = Convert.ToInt32 ( str ); //atol ( str ) ; if ( PointW <= PricePointW ) { for ( int i = 0 ; i < ( PricePointW - PointW ) ; i++ ) lPrice *= 10; } else { for ( int i = 0 ; i < ( PointW - PricePointW ) ; i++ ) lPrice /= 10; } } } catch ( Exception e ) { FrontFlag.CONTROL ctrl = new CONTROL () ; ctrl.MsgBox.Show ( "Err at Str2Price() function" ); } return lPrice; }