Solved

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

Posted on 2014-11-22
7
161 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now