skip to Main Content

So I’m pulling an array of collections from mongoDB and one of the documnets in the collection is a "price" document saved in the DB as a number (int32).

const hotAssets = await Asset.find({ hotAsset: true });

Now I feed the array to a function that converts the price documnet to String and add commas every 3 numbers. The weird thing is that the documnet just won’t convert no matter what I do.

The function call

Utils.assetPriceToString(hotAssets);

The function

exports.assetPriceToString = (assetsArray) => {
  for (const asset of assetsArray) {
    if (asset.price) asset.price = asset.price.toString().replace(/B(?=(d{3})+(?!d))/g, ',');
  }
};

example of a collection

{
    location: {
      coordinates: [Array],
      title: 'Central Park Burgas',
      type: 'Point'
    },
    _id: new ObjectId('66420dbd1ada8e8e709e30a7'),
    id: 3698,
    name: 'new flat',
    slug: 'new-flat',
    price: 120000,
    project: 'Central Park Burgas',
    city: 'Burgas',
    type: 'דירה',
    sm: '82 מ"ר',
    oceanView: '',
    rooms: '2',
    bathrooms: 1,
    terraces: '1',
    floor: '18 מ 20',
    readiness: '',
    serviceTax: '',
    description: 'some descriptin', 
    year: 2008,
    mainImage: '/img/asset1.jpg',
    images: [
      '/img/asset1.jpg',  '/img/asset2.jpg',
      '/img/asset3.jpg',  '/img/asset4.jpg',
      '/img/asset5.jpg',  '/img/asset6.jpg',
      '/img/asset7.jpg',  '/img/asset8.jpg',
      '/img/asset9.jpg',  '/img/asset10.jpg',
      '/img/asset11.jpg', '/img/asset12.jpg',
      '/img/asset13.jpg', '/img/asset14.jpg',
      '/img/asset15.jpg', '/img/asset16.jpg',
      '/img/asset17.jpg', '/img/asset18.jpg',
      '/img/asset19.jpg', '/img/asset20.jpg',
      '/img/asset21.jpg', '/img/asset22.jpg',
      '/img/asset23.jpg'
    ],
    hotAsset: true,
    updated_at: 2024-05-13T12:55:24.545Z,
    __v: 0,
    priceNis: 482027
  }

Iv’e tried hard coding another array with objects that have "price" variables and it worked fine.

Iv’e tried to convert using asset.price = String(asset.price) or asset.price = ” + asset.price but it did not convert.

This does not work as well. if i consol.log(new Intl.NumberFormat().format(asset.price)); it logs as a String, same as if I console.log(asset.price.toString()) but when I try to convert it does not work.

asset.price = new Intl.NumberFormat().format(asset.price); 

Please help.

Tried to convert number to string but it just won’t convert.

2

Answers


  1. Instead of manually trying to format as a $x,xxx.xx format, use the built-in functions.

    Define your formatting options and then format the number:

    let n = 12000;
    console.log( new Intl.NumberFormat('en-CA', {style: 'currency', currency: 'CAD'}).format(n));
    Login or Signup to reply.
  2. Your code works just fine for me. Try it in this code snippet:

    function testOnClick( event ) {
      assetPriceToString(hotAssets);
    }
    
    
    let assetPriceToString = (assetsArray) => {
      for (const asset of assetsArray) {
        if (asset.price) {
          console.log( `Before: asset.price ${asset.price} is a ${typeof asset.price}`)
          asset.price = asset.price.toString().replace(/B(?=(d{3})+(?!d))/g, ',');
          console.log( `After: asset.price ${asset.price} is a ${typeof asset.price}`)
        }
      }
    }
    
    
    let hotAssets = [
      {
        location: {
          coordinates: [Array],
          title: 'Central Park Burgas',
          type: 'Point'
        },
        // _id: new ObjectId('66420dbd1ada8e8e709e30a7'),
        _id: "new ObjectId('66420dbd1ada8e8e709e30a7')",
        id: 3698,
        name: 'new flat',
        slug: 'new-flat',
        price: 120000,
        project: 'Central Park Burgas',
        city: 'Burgas',
        type: 'דירה',
        sm: '82 מ"ר',
        oceanView: '',
        rooms: '2',
        bathrooms: 1,
        terraces: '1',
        floor: '18 מ 20',
        readiness: '',
        serviceTax: '',
        description: 'some descriptin',
        year: 2008,
        mainImage: '/img/asset1.jpg',
        images: [
          '/img/asset1.jpg',  '/img/asset2.jpg',
          '/img/asset3.jpg',  '/img/asset4.jpg',
          '/img/asset5.jpg',  '/img/asset6.jpg',
          '/img/asset7.jpg',  '/img/asset8.jpg',
          '/img/asset9.jpg',  '/img/asset10.jpg',
          '/img/asset11.jpg', '/img/asset12.jpg',
          '/img/asset13.jpg', '/img/asset14.jpg',
          '/img/asset15.jpg', '/img/asset16.jpg',
          '/img/asset17.jpg', '/img/asset18.jpg',
          '/img/asset19.jpg', '/img/asset20.jpg',
          '/img/asset21.jpg', '/img/asset22.jpg',
          '/img/asset23.jpg'
        ],
        hotAsset: true,
        // updated_at: 2024-05-13T12:55:24.545Z,
        updated_at: "2024-05-13T12:55:24.545Z",
        __v: 0,
        priceNis: 482027
      }
    ]
          <button type="button" onclick="testOnClick(event)">Test</button>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search