json2csv icon indicating copy to clipboard operation
json2csv copied to clipboard

Conditionnal decimal formatter ?

Open popod opened this issue 4 months ago • 2 comments

Hello,

How to only apply formatters to some fields ?

In the exemple bellow, the price field should be formatted with a "," separator, but not the average field ?

// data
[{
    title: "title",
    price: 19.45
    average: 2.9849302
}, {
   ...
}]
// wanted csv
"title";"price";"average"
"title";10,45;"2.9849302"
...

Before, i was using the removed option stringify.

popod avatar Aug 22 '25 09:08 popod

Hi @popod ,

Can you give me a code sample to reproduce the issue?

juanjoDiaz avatar Aug 25 '25 18:08 juanjoDiaz

Here are some code sample

import { Parser } from '@json2csv/plainjs';
import { number as numberFormatter } from '@json2csv/formatters';

const data = [{
  name: 'name1',
  price: 10.50,
  average: 2.8394
  
}, {
  name: 'name2',
  price: 8.35,
  average: 5.333333
}];

const parser = new Parser({
  delimiter: ';',
  formatters: {
    number: numberFormatter({ separator: ',' }),
  }
});
const csv = parser.parse(data);

console.log(csv);

This output is

"name";"price";"average"
"name1";10,5;2,8394
"name2";8,35;5,333333

But i want that the output is

"name";"price";"average"
"name1";10,5;2.8394
"name2";8,35;5.333333

-> price use "," separator for decimal, but average use ".".

popod avatar Aug 29 '25 13:08 popod