Solved

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

Posted on 2014-11-22
7
167 Views
Last Modified: 2014-11-29
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
Comment
Question by:Delphiwizard
  • 4
7 Comments
 
LVL 20

Expert Comment

by:dsacker
ID: 40460100
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
 

Author Comment

by:Delphiwizard
ID: 40460336
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
 

Accepted Solution

by:
Delphiwizard earned 0 total points
ID: 40460339
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 40461826
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
 

Author Comment

by:Delphiwizard
ID: 40461877
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
 

Author Closing Comment

by:Delphiwizard
ID: 40471431
It's the solution that works in this situation.
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question