fri变量,只能包含有 小数点、空格、 '/'、数字。如 1 1/2 或1.2 function fri2decimal(fri: string): double; var strs: TStrings; fri_dec: double; function csubstr(substr: string; str: string): integer; var i, j: integer; begin j := 0; for i := 1 to length(str) do if str[i] = substr then j := j + 1; result := j; end;
function onlydecimal(str_fri: string): double; begin //2.只有小数点、没有空格、'/' 如有只一个小数点,则直接转换,如有多个小数点,全部去掉后转换 if csubstr('.', str_fri) <> 1 then //一个小数点的保留 str_fri := StringReplace(str_fri, '.', '', [rfReplaceAll]); //多个小数点全去掉 result := strtofloat(str_fri); end;
function onlyspace(str_fri: string): double; begin //3.只有空格、没有小数点、'/' 去除全部空格后转换 str_fri := StringReplace(str_fri, ' ', '', [rfReplaceAll]); result := strtofloat(str_fri); end;
function onlyxg(str_fri: string): double; begin //4.只有'/'、没有小数点、空格 ,如'/'在头,在尾,或多个都全部去掉,只有一个的进行除运算,如分母为0的,仅对分子进行转换 //如果/在头与尾,或超过1个则去掉/ if (pos('/', str_fri) = 1) or (pos('/', str_fri) = length(str_fri)) or (csubstr('/', str_fri) <> 1) then begin str_fri := StringReplace(str_fri, '/', '', [rfReplaceAll]); result := strtofloat(str_fri); end else begin //如果在中间,进行除法运算 strs := TStringList.Create; strs.Delimiter := '/'; strs.DelimitedText := str_fri; if strtofloat(strs[1]) = 0 then //如分母为0的,仅对分子进行转换 result := strtofloat(strs[0]) else result := roundto(strtofloat(strs[0]) / strtofloat(strs[1]), -5); strs.Free; end; end;
function spacexg(str_fri: string): double; begin if (pos('/', str_fri) = 1) or (pos('/', str_fri) = length(str_fri)) or (csubstr('/', str_fri) <> 1) then //如果/在最前与最后,去掉/ begin ///在最前与最后,或多个/去掉/ str_fri := StringReplace(str_fri, '/', '', [rfReplaceAll]); result := onlyspace(str_fri); end else if (pos('/', str_fri) < pos(' ', str_fri)) or (csubstr(' ', str_fri) <> 1) then //如果/在空格前面,去除空格,按分数计算 begin //如果/在空格前,或多个空格 去掉空格 str_fri := StringReplace(str_fri, ' ', '', [rfReplaceAll]); result := onlyxg(str_fri); end else begin strs := TStringList.Create; strs.Delimiter := ' '; strs.DelimitedText := str_fri; result := strtofloat(strs[0]) + onlyxg(strs[1]); end; end; begin result := 0; //1.空格、小数点、'/' 都没有 直接转换为小数 if (pos(' ', fri) = 0) and (pos('.', fri) = 0) and (pos('/', fri) = 0) then result := strtofloat(fri); //2.只有小数点、没有空格、'/' 如有只一个小数点,则直接转换,如有多个小数点,全部去掉后转换 if (pos(' ', fri) = 0) and (pos('.', fri) <> 0) and (pos('/', fri) = 0) then result := onlydecimal(fri); //3.只有空格、没有小数点、'/' 去除全部空格后转换 if (pos(' ', fri) <> 0) and (pos('.', fri) = 0) and (pos('/', fri) = 0) then result := onlyspace(fri); //4.只有'/'、没有小数点、空格 ,如'/'在头,在尾,或多个都全部去掉,只有一个的进行除运算,如分母为0的,仅对分子进行转换 if (pos(' ', fri) = 0) and (pos('.', fri) = 0) and (pos('/', fri) <> 0) then result := onlyxg(fri); //5.有小数点 有空格,没有'/' 去掉空格,按小数来来计算 if (pos(' ', fri) <> 0) and (pos('.', fri) <> 0) and (pos('/', fri) = 0) then begin fri := StringReplace(fri, ' ', '', [rfReplaceAll]); result := onlydecimal(fri); end;
//6.有'/',有小数点 没空格, 去掉小数点,按/来来计算 if (pos(' ', fri) = 0) and (pos('.', fri) <> 0) and (pos('/', fri) <> 0) then begin fri := StringReplace(fri, '.', '', [rfReplaceAll]); result := onlyxg(fri); end;
//7.有'/',有空格 没小数点, if (pos(' ', fri) <> 0) and (pos('.', fri) = 0) and (pos('/', fri) <> 0) then result := spacexg(fri); //8.有'/',有空格 有小数点, 去除小数点,按7来算 if (pos(' ', fri) <> 0) and (pos('.', fri) <> 0) and (pos('/', fri) <> 0) then begin fri := StringReplace(fri, '.', '', [rfReplaceAll]); result := spacexg(fri); end; end; |