ofxparser icon indicating copy to clipboard operation
ofxparser copied to clipboard

Regex amount parsing wrong

Open oceanapplications opened this issue 8 years ago • 8 comments

The function createAmountFromStr($amountString) seems to have some bugs with strings that aren't number formatted and have one or more trailing 0s. For example, 750 will parse as 7.5 but 50 parses fine.

Here's a playground link demonstrating the issue. http://tehplayground.com/#SSeFA3M80

Thanks

oceanapplications avatar Jun 20 '16 05:06 oceanapplications

This is a BC break (even if it's preferable), and we have not nearly enough unit test coverage to explicitly show the breakages. Will need to think on this!

asgrim avatar Jun 20 '16 08:06 asgrim

I was thinking something like this at the top of the function might solve the problem. if (strpos($amountString, '.') === FALSE && strpos($amountString, ',') === FALSE) { $amountString .= ".00"; }

The OFX spec 2.2, page 91 http://www.ofx.net/downloads/OFX%202.2.pdf states:

Amounts that do not represent whole numbers (for example, 540.32), must include a decimal point or comma to indicate the start of the fractional amount. Amounts should not include any punctuation separating thousands, millions, and so forth.

Are files breaking the standard format common?

oceanapplications avatar Jun 20 '16 20:06 oceanapplications

They rarely conform to specs, sadly! There's lots of edge cases taken into account, hence why we could really do with some more unit tests.

asgrim avatar Jun 20 '16 22:06 asgrim

Note: tests showing this weird behaviour are commented with @todo now: https://github.com/asgrim/ofxparser/blob/master/tests/OfxParser/OfxTest.php#L40

asgrim avatar Aug 21 '16 20:08 asgrim

As this is a BC break, I'm going to schedule this for 2.0.

asgrim avatar Aug 23 '16 16:08 asgrim

We're seeing the same problem when the amount is formatted as 1.00000, it's converted to 1000 rather than 1.

hillelcoren avatar Oct 03 '17 17:10 hillelcoren

Same problem over here. -699 is converted to -6.99, -2000 to -20.00

frederichoule avatar Dec 31 '17 20:12 frederichoule

Same problem here i solved with

       $handle = $this->openFile();
        while (($line = fgets($handle)) !== false) {
            $line = trim($line);
            if (starts_with($line, "<TRNAMT>") && ends_with($line, "00")){
                $line.="00";
            }
            $this->fixed .= $line."\n";
        }
        $parser = new \OfxParser\Parser();
        $ofx = $parser->loadFromString($this->fixed);

guifabrin avatar May 11 '19 05:05 guifabrin