Skip to content

Instantly share code, notes, and snippets.

@wyster
Created April 23, 2016 20:25
Show Gist options
  • Save wyster/b1804a1bf544af8bdbdacfc30e021b7a to your computer and use it in GitHub Desktop.
Save wyster/b1804a1bf544af8bdbdacfc30e021b7a to your computer and use it in GitHub Desktop.
function priceFormatter(priceText) {
"use strict";
priceText = priceText.replace('.', '').trim();
priceText = priceText.replace(',', '.').trim();
const re = /(\D*)(.*)/;
const result = re.exec(priceText);
var price = parseFloat(parseFloat(result[2]).toFixed(2));
return price;
}
const test = require('tape');
test('price formatting test', function (t) {
t.plan(2);
t.equal(parseFloat('1357.14'), priceFormatter('1.357,14'));
t.equal(parseFloat('109.99'), priceFormatter('109,99'));
});
TAP version 13
# price formatting test
ok 1 should be equal
ok 2 should be equal
1..2
# tests 2
# pass 2
# ok
function priceFormatter(priceText) {
"use strict";
priceText = priceText.replace(',', '.').trim();
const re = /(\D*)(.*)/;
const result = re.exec(priceText);
var price = parseFloat(parseFloat(result[2]).toFixed(2));
return price;
}
const test = require('tape');
test('price formatting test', function (t) {
t.plan(2);
t.equal(parseFloat('1357.14'), priceFormatter('1.357,14'));
t.equal(parseFloat('109.99'), priceFormatter('109,99'));
});
TAP version 13
# price formatting test
not ok 1 should be equal
---
operator: equal
expected: 1.36
actual: 1357.14
at: Test.<anonymous> (/home/ilya/web/amazon-wish-list2/src/priceFormatter.js:20:7)
...
ok 2 should be equal
1..2
# tests 2
# pass 1
# fail 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment