That would be one way to do it.
Keep in mind that modeling a number as a string a string potentially
make things harder for the consumer of the infoset. For example, if your
data could contain commas (e.g. 5,000.00), the infoset consumer needs to
know that the value might have commas in it if they want to use it like
a number, so requires extra processing for them. If you treat is as a
number, those commas are stripped out and using the value as a number is
much easier.
You also lose validation that is provided by number parsing that ensures
things like all the digits are valid numbers, commas are correct, etc.
Though, you could probably use a restriction pattern or perhaps an
assert to validate that the string represents a valid number.
Another option might for Daffodil to implement the fn:format-number
function. Then you could use that and inputValueCalc to define exactly
how you want a number to appear in the infoset. However,
fn:format-number isn't part of the DFDL spec, so such a schema might not
be portable unless it was added to the spec.
Without that function, you could do something like below, which is more
complex, but supported by Daffodil today:
<xs:element name="value" type="xs:decimal" ... />
<xs:element name="valueString" type="xs:string" dfdl:inputValueCalc="{
if (fn:not(fn:contains(../value, '.')))
then fn:concat(../value, '.00')
else if (fn:string-length(fn:substring-after(../value, '.')) eq 1)
then fn:concat(../value, '0')
else
xs:string(../value)
}" />
So if the string representation of the decimal value does not contain a
dot then it appends .00. If it only contains one number after a dot
(e.g. 5000.1) then it appends a single zero, otherwise it just uses the
string representation of the value.
- Steve
On 12/3/18 9:41 AM, Costello, Roger L. wrote:
> Hi Steve,
>
>> In the case of decimal numbers, Daffodil creates
>> an infoset output with the minimum number of
>> digits necessary to display it with the same precision.
>> So 5000.00 will be output as 5000
>
> If I want to retain the digits to the right of the decimal point, then I
> should declare the price element with the type xs:string, is that correct?
>
> /Roger
>
> -----Original Message-----
> From: Steve Lawrence <[email protected]>
> Sent: Monday, December 3, 2018 9:36 AM
> To: [email protected]; Costello, Roger L. <[email protected]>
> Subject: Re: How to retain the digits to the right of the decimal point?
>
> The pattern defines the format of the data. It does not define the format of
> the infoset. I believe the spec is ambiguous or silent about how various data
> fields should be output to the infoset. I know we've had this issue with
> date/time fields recently.
>
> In the case of decimal numbers, Daffodil creates an infoset output with the
> minimum number of digits necessary to display it with the same precision. So
> 5000.00 will be output as 5000, but 5000.99 will be output with the extra
> decimal precision.
>
> - Steve
>
> On 12/3/18 9:30 AM, Costello, Roger L. wrote:
>> Hi Mike,
>>
>> * Use 0 instead of # for the rightmost two.
>>
>> I tried that:
>>
>> <xs:elementname="price"type="xs:decimal"
>> dfdl:textStandardDecimalSeparator="."
>> dfdl:textNumberPattern="####.00"/>
>>
>> It gave the same result (the .00 is removed):
>>
>> 5000.00 --> parse --> 5000
>>
>> Thoughts?
>>
>> /Roger
>>
>> *From:* Mike Beckerle <[email protected]>
>> *Sent:* Monday, December 3, 2018 9:21 AM
>> *To:* [email protected]
>> *Subject:* Re: How to retain the digits to the right of the decimal point?
>>
>> Use 0 instead of # for the rightmost two. In a pattern, a zero denotes
>> any digit. A # denotes an optional digit.
>>
>> -------- Original message --------
>>
>> From: "Costello, Roger L." <[email protected]
>> <mailto:[email protected]>>
>>
>> Date: 12/3/18 8:46 AM (GMT-05:00)
>>
>> To: [email protected] <mailto:[email protected]>
>>
>> Subject: How to retain the digits to the right of the decimal point?
>>
>> Hello DFDL community,
>>
>> My input contains decimal values such as: 2999.99 and 5000.00
>>
>> When I parse my input, the .00 gets removed, e.g.,
>>
>> 5000.00 --> parse --> 5000
>>
>> But the .99 is not removed, e.g.,
>>
>> 2999.99 --> parse --> 2999.99
>>
>> I want to retain the two digits to the right of the decimal point,
>> even if they are 00
>>
>> How to retain the digits? I thought this would do the job:
>>
>> dfdl:textNumberPattern="####.##"/>
>>
>> However, that has no effect.
>>
>> What's the right way to do it?
>>
>> /Roger
>>
>