Mega Code Archive

 
Categories / Delphi / Functions
 

Strtocurr - convert a number string into a currency value sysutils unit

1 function StrToCurr ( CurrencyString : string ) : Currency; 2 function StrToCurr ( CurrencyString : string; const FormatSettings : TFormatSettings ) : Currency; Description The StrToCurr function converts a number string, CurrString such as '123.456' into a Currency value. It supports integer, floating point, and scientific (exponent) formats. If a decimal point appears in CurrString, then it must match the current DecimalSeparator value. Version 2 of this function is for use within threads. You furnish the FormatSettings record before invoking the call. It takes a local copy of global formatting variables that make the routine thread safe. Notes The EConvertError exception is thrown if there are errors in CurrString, such as trailing blanks or invalid decimal characters. Related commands Currency A floating point type with 4 decimals used for financial values CurrToStr Convert a currency value to a string CurrToStrF Convert a currency value to a string with formatting Format Rich formatting of numbers and text into a string TFormatSettings A record for holding locale values for thread-safe functions Example code : Converting a scientific format number string var stringValue : string; currValue : Currency; begin // Set up the source string containing a number representation stringValue := '123.456E+002'; // Convert it to a floating point number currValue := StrToCurr(stringValue); // And display the value ShowMessage(stringValue+' = '+CurrToStr(currValue)); end; Show full unit code 123.456E+002 = 12345.6 Example code : Catching string conversion errors var A : Currency; begin // We will catch conversion errors try A := StrToCurr('10 E 2'); // Middle blanks are not supported except on Exception : EConvertError do ShowMessage(Exception.Message); end; try A := StrToCurr('$FF'); // Hexadecimal values are not supported except on Exception : EConvertError do ShowMessage(Exception.Message); end; end; Show full unit code '10 E 2' is not a valid floating point value '$FF' is not a valid floating point value