Solved

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

Posted on 2014-11-22
7
158 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 25

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.

706 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

17 Experts available now in Live!

Get 1:1 Help Now