简体   繁体   English

在Crystal Reports中将字符串数字转换为十进制时出错:“字符串为非数字”

[英]Error when converting string number to decimal in Crystal Reports: “The string is non-numeric”

Problem description 问题描述

In my report I get amounts in strings. 在我的报告中,我得到的是字符串形式的金额。 The formatting is pretty unusual: 格式非常不寻常:

  1. "" - is zero amount “” -为零
  2. "(200.00)" - is negative amount “(200.00)” -为负数
  3. "1,234.56" - is positive amount “ 1,234.56” -为正数

I want to convert those strings to numeric values in more convenient way: 我想以更方便的方式将这些字符串转换为数值:

  1. 0.00 0.00
  2. -200.00 -200.00
  3. 1234.56 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 然后我想使用ToNumberCDbl方法将字符串转换为数字,但是两者都导致相同的错误

// "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 问题

  1. How could I fix my string amount to make ToNumber and CDbl works fine? 如何确定我的字符串数量以使ToNumberCDbl正常工作?
  2. How can I convert string amount to decimal number without using ToNumber or CDbl methods? 如何在不使用ToNumberCDbl方法的情况下将字符串金额转换为十进制数字?

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 我准备了特定的测试,以查看字符串值中是否有任何意外的字符,但是以下比较的所有结果均返回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 我正在使用Crystal Reports 2013

My idea to solve this problem was to split the string amounts on integer and fractional part, convert them to numbers separately and then add both to get the value. 解决这个问题的我的想法是将字符串数量分成整数和小数部分,分别将它们转换为数字,然后将两者相加以获得值。

local stringvar amount := Trim({PLD__ITEMS.F_18});
local stringvar intAmount := '0';
local stringvar decAmount := '0';
local numbervar result;
local numbervar decimal;


if amount = ''
    then amount := '0.00'
;

amount := Replace(amount, "(", "-");
amount := Replace(amount, ")", "");
amount := Replace(amount, ",", "");
amount := Replace(amount, " ", "");

if InStr(amount, '.') > 0
    then (
        intAmount := Left(amount, InStr(amount, '.') - 1);
        decAmount := Right(amount, len(amount) - InStr(amount, '.'));
    )
    else intAmount := amount
;

result := ToNumber(intAmount);
decimal := ToNumber(decAmount) / 100;

if result > 0
    then result := result + decimal
    else result := result - decimal
;

Although this tricky solution works fine, my questions are still open for ideas and/or advices. 尽管这个棘手的解决方案很好用,但我的问题仍然有想法和/或建议。

updated 更新

Fix solution for negative numbers - fractional part should be substracted from integer part 修复负数的解决方案-小数部分应从整数部分中减去

I found that Crystal using comma as decimal separator. 我发现Crystal使用逗号作为小数点分隔符。 (That was unexpected part for me!) (这对我来说是意外的部分!)
Below code works fine for me: 下面的代码适合我:

ToNumber('1,00')

Since now the solution was easy: 从现在开始,解决方案很简单:

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, ".", ",");
amount := Replace(amount, " ", "");

ToNumber(amount)

Above solves my problem with Crystal Reports report. 上面用Crystal Reports报告解决了我的问题。 The values of amounts displayed in Crystal Reports Preview were OK, but when I upload my report to SAP B1 application, the amounts times by 100. So... 在Crystal Reports预览中显示的金额值还可以,但是当我将报表上载到SAP B1应用程序时,金额乘以100。所以...

在此处输入图片说明

在此处输入图片说明

It looks like convertions is wrong or SAP B1 interpretation of Crystal Report is crashed... 似乎转换错误或Crystal Report的SAP B1解释崩溃了...

Ugh! 啊! What a mess... 真是一团糟...

So I came back to previous solution with separately converted integer and fractional part, which produce correct amounts on both Crystal Reports preview and SAP B1 application. 因此,我回到了以前的解决方案,其中包含分别转换的整数和小数部分,它们在Crystal Reports预览和SAP B1应用程序上都能产生正确的金额。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 水晶报告公式中的“字符串是非数字”错误 - 'The string is non-numeric' error in a crystal reports Formula 水晶报表中的字符串是非数字异常 - String is non-numeric Exception in crystal report 在Crystal Reports中将字符串转换为日期 - Converting string to date in Crystal Reports 将Crystal报告运行总字符串转换为数字时出现问题 - Problems converting a crystal reports running total string to a number 让Crystal Reports公式将数字字符串转换为值,但保留非数字空白/ null - Have a Crystal Reports formula convert numeric strings to values, but leave non-numeric blank/null 水晶报表返回字符串或数字 - Crystal reports return string or number 如何在 Crystal Reports 2008 中将数字字符串值格式化为 2 个小数位? - How do I format numeric string value to 2 decimal places in Crystal Reports 2008? 公式中没有错误,但是当我进入某些页面时,出现“字符串非数字”错误,并且我无法再预览报告 - No error in formula but when I get to certain page I get a “String is non-numeric” error and I can no longer preview the report Crystal Reports Issue将字符串转换为数字 - Crystal Reports Issue turning a string into a number 数字报表中的数字公式是否为小数 - Formula for number is decimal or not in Crystal Reports
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM