Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-11-22
7
Medium Priority
?
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
LVL 27

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

688 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