Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Two-decimal value get's floating with multiple decimal positions in XML

Hi,

In my Delphi XE2 project I select data from SQL Server as follows:
SELECT CONVERT(DECIMAL(28, 2), ROUND(SUM(ISNULL((Amount * -1), 0)), 2)) AS Amount
FROM MyTable

Open in new window

Query1.FieldByName('Amount').AsCurrency has value with two decimals: f.e. 0.21

Then I assign it to an XML value:
xTransL.Amount.Value             := Query1.FieldByName('Amount').AsCurrency;

Open in new window

Now in the XML value 0.21 is transformed into value 0.209999993443489
<Amount>
    <Currency code="EUR"/>
    <Value>0,209999993443489</Value>
</Amount>

Open in new window

In XML Data Binding xTransL.Amount.Value is declared as:
property Value: Single read Get_Value write Set_Value;

Open in new window


How can I get the correct value 0.21 into the XML?
0
Stef Merlijn
Asked:
Stef Merlijn
  • 4
1 Solution
 
dsackerContract ERP Admin/ConsultantCommented:
I added one more CONVERT function in the SQL to treat it as a character string. Now, you should be able to treat it as a string in your code and be just fine.
SELECT CONVERT(varchar(20), CONVERT(DECIMAL(28, 2), ROUND(SUM(ISNULL((Amount * -1), 0)), 2))) AS Amount
FROM MyTable

Open in new window

0
 
Stef MerlijnDeveloperAuthor Commented:
But I can't assign a String-value to a Single, so this doesn't realy help me.

If looked a bit further and found that I can round the value just before it is assigned to the XML-node.
procedure TXMLAmount.Set_Value(Value: Single);
begin
  ChildNodes['Value'].NodeValue := DecimalRoundSgl(Value, 2, drHalfUp);
end;

Open in new window


Now I need to replace the decimalseparator "," with a ".".

Is there a way to update the nodevalue after I assigned it?
0
 
Stef MerlijnDeveloperAuthor Commented:
Found it:
procedure TXMLAmount.Set_Value(Value: Single);
begin
  ChildNodes['Value'].NodeValue := DecimalRoundSgl(Value, 2, drHalfUp);
  ChildNodes['Value'].NodeValue := StringReplace(ChildNodes['Value'].NodeValue, ',', '.',[rfReplaceAll]);
end;

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Sinisa VukCommented:
you can try set TFloatField's display format:
//after you open query ....
TFloatField(data.FieldByName('Value')).DisplayFormat := '0.00';  //set float format

Open in new window

0
 
Stef MerlijnDeveloperAuthor Commented:
Sinisa Vuk: No, that doesn't seem to work. The moment the value is assigned to the XML-value (Single) it is messed up.
My solution works fine for me.
0
 
Stef MerlijnDeveloperAuthor Commented:
It's the solution that works in this situation.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now