Problem description
In my report I get amounts in strings. The formatting is pretty unusual:
- "" - is zero amount
- "(200.00)" - is negative amount
- "1,234.56" - is positive amount
I want to convert those strings to numeric values in more convenient way:
- 0.00
- -200.00
- 1234.56
First I am doing some preformattings of the string amount:
local stringvar amount := Trim({PLD__ITEMS.F_18});
if amount = ''
then amount := '0.00'
;
amount := Replace(amount, "(", "-");
amount := Replace(amount, ")", "");
amount := Replace(amount, ",", "");
amount := Replace(amount, " ", "");
Then I wanted to convert string into number using ToNumber or CDbl methods, but both result with the same error
// "The string is non-numeric"
//ToNumber(amount)
// "The string is non-numeric"
//CDbl(amount)
I has no idea what could possibly cause this error.
I can't find any corrupted string in the formatted amounts...
Questions
- How could I fix my string amount to make ToNumber and CDbl works fine?
- How can I convert string amount to decimal number without using ToNumber or CDbl methods?
If there was only displaying issue, I could use strings as there are, but I need to do some calculations with those amounts so I have to use numeric values there.
Testing unexpected characters in string amount
I prepared specific test to see if any of string amount value has unexpected character inside, but all results of below comparision returned True
// ---- test ----
amount := Replace(amount, "0", "");
amount := Replace(amount, "1", "");
amount := Replace(amount, "2", "");
amount := Replace(amount, "3", "");
amount := Replace(amount, "4", "");
amount := Replace(amount, "5", "");
amount := Replace(amount, "6", "");
amount := Replace(amount, "7", "");
amount := Replace(amount, "8", "");
amount := Replace(amount, "9", "");
amount := Replace(amount, ".", "");
amount := Replace(amount, "-", "");
// has not unexpected characters
amount = ''
// ---- end test ----
Testing convertion
I tested explicit converion of string with point as decimal separator and again error occured (what is strange for me)!
I am using Crystal Reports 2013