d-money ctor(string) has bug, be careful! EUR("1.23456") ==> ctor ==> 3.3456EUR

mw mingwu at gmail.com
Sun May 24 19:17:03 UTC 2020


On Sunday, 24 May 2020 at 18:54:55 UTC, Basile B. wrote:
> On Sunday, 24 May 2020 at 18:30:38 UTC, mw wrote:
>> On Sunday, 24 May 2020 at 18:15:09 UTC, Basile B. wrote:
>>> On Sunday, 24 May 2020 at 17:06:43 UTC, mw wrote:
>>>> On Friday, 22 May 2020 at 05:26:45 UTC, mw wrote:
>>>>>[...]
>>>>
>>>> For anyone who's using this package, just submitted the PR 
>>>> to fix it:
>>>>
>>>> https://github.com/qznc/d-money/pull/12
>>>
>>> there's also parsing that is not super compliant
>>>
>>> EUR y = EUR("1.000.000,00");
>>> writeln(y);                     // F!
>>
>> Added comment here on git:
>>
>> https://github.com/qznc/d-money/pull/12#issuecomment-633272988
>>
>> =====================================
>> The parsing regex used internally is a very simple one:
>> https://github.com/qznc/d-money/blob/master/source/money.d#L98
>>
>> I don't want to enhanced it to be a full sscanf().
>>
>> This is just a maintenance bug fix.
>
> No this is an issue on itw own. The thousands and the decimal 
> separator should be template parameters or customizable in some 
> way.

As said, this is maintenance fix, I don't want to change the way 
how the original package is designed before, and add template 
parameters.

But for your case, I added filter out commas ',':

https://github.com/qznc/d-money/pull/12/commits/ff7aaa2b70dc91e2f93a5a26ecd04185de273f3a

	auto y = EUR("1,000.000,00");
	auto z = EUR("1000");
	// writeln(y);
	assert(y == z);


BTW, the new ctor throw out exception on bad input it cannot 
recognize:
money.ParseError@!isNumeric: 1.000.000,00(105): Parse error

is correct behavior.



More information about the Digitalmars-d mailing list