DelphiBasics
StrToCurr
Function
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
CurrencyA decimal type with 4 decimal points used for financial values
CurrToStrConvert a currency value to a string
CurrToStrFConvert a currency value to a string with formatting
FormatRich formatting of numbers and text into a string
TFormatSettingsA record for holding locale values for thread-safe functions
 Download this web site as a Windows program.




 
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
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page