Storing numbers with a thousand separator in a JavaScript Array: tips and tricks

I need to save an Array in this format

const price = [ 1.000, 24.500, 3.99, 4.00 ];

However, when I use console.log to print it, the numbers get truncated to integers (1 instead of 1.000, and 4 instead of 4.00).

What's the best way to preserve the decimal points and keep the thousand separator?

Answer №1

Check out this sample code:

console.log(price.toFixed(3));

edit-1:


prices.forEach((price)=>{ console.log(price.toFixed(3)); })

let truePrice = prices.map( (price)=> price.toFixed(3) )

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Synchronizing two navigation menus on a single-page application website

Let me start by saying that I specialize in back end development and am facing a specific challenge with building a website that includes two navigation menus. The main navigation menu features links like Home, while the sub-navigation menu includes option ...

Navigating Through Objects

Currently, I am utilizing the Jquery Dropdown Plugin and ListJS Plugin for my project. The jQuery Dropdown Plugin features a hide event: $('.dropdown').on('hide', function(event, dropdownData) { }); Within this event, I am integratin ...

How to prevent checkbox autocomplete from selecting the previously checked value using Jquery Ajax

Working on implementing the "Autocomplete ajax search" feature using Php. Successfully fetching data from the database. Currently, when searching for something, the results with checkboxes are displayed. However, when I search for a text, check a checkbo ...

What is the best way to utilize Office365 Excel array formulas to find the minimum and maximum values for each individual?

Looking at the test data provided below, how can we design the array formula to achieve the expected outcome? =LET(item,G4,name,A4:A7,data,C4:E15, names, CHOOSECOLS(data,1), items, CHOOSECOLS(data,2), cnt, CHOOSECOLS(data,3), mn, MIN(I ...

Removing an element from an array in a Laravel database using AngularJS

I am currently working on a project where I need to delete an item from my Laravel database, which is a simple Todo-List application. To achieve this, I have implemented a TodosController in my main.js file using AngularJS and created a Rest API to connect ...

What is the best way to select the initial element from my class within my component using protractor?

Can anyone help me figure out how to click on the button in this component? I need guidance on navigating through the following path: Is xpath my only option for doing this? I believe a css locator could work as well, but I am unsure of how to construct ...

Remove an item from the DOM instantly with React

Having trouble synchronously removing a child from the container? Here is a simplified code snippet demonstrating the current solution using the useState hook. type ChildProps = { index: number; id: string; remove: (index: number) => void; }; fun ...

What's the most effective method for updating the title of the date header on MUI Date Picker?

Does anyone know how to customize the title of the Calendar in MUI Date Picker? I want to add a specific string to the display that shows the month and year, like "August 2014." https://i.stack.imgur.com/qgMun.png I've searched the API but couldn&ap ...

Error encountered while utilizing the infinite-react-carousel package in React

After entering "npm i infinite-react-carousel --save" into the terminal, an error message appears: "npm ERR! code ERESOLVE "npm ERR! ERESOLVE unable to resolve dependency tree" What could be causing this issue? How can I troubleshoot and resolve it? I h ...

Stop Bootstrap 5 Accordion from collapsing

I have successfully implemented the Accordion component in Bootstrap 5, and it is working perfectly! However, I am facing an issue with the size of the collapse button in the accordion, which is too big. I want to add an additional link to it for some ext ...

Enhance the display of additional details on the Agenda using fullcalendar_rails

Can you please provide the title and date of the event, but I also need additional fields to be displayed? By modifying the index.json.jbuilder like this: (json.title event.user.email), instead of showing the title it displays the email. However, I req ...

The height of the Material UI Paper component is not appropriately matched with the parent component

I am currently working with the Paper component that contains a Card component, and I am trying to make its height fill the entire screen. To simplify the problem, I have provided the following code: import React from "react"; import { makeStyles ...

Storing a collection of objects in session storage

I've been struggling to save an array containing the items in my online shopping cart. Even though both the object and the array are being filled correctly, when I check the sessionStorage, it shows an array with an empty object. I've spent a lot ...

Fixing a div at the top post scroll - bug on iOS mobile device

I am looking to achieve a similar effect as demonstrated in the example below: https://css-tricks.com/scroll-fix-content/ Essentially, the goal is to have a div become fixed at the top of the page after scrolling to a certain point. Initially, the div wil ...

What are the steps to enable a Vue component to handle its own transitions?

I am looking for a way to handle enter and leave animations in Vue when components are mounted or removed. My goal is to consolidate all the animation code into its own component for better organization. <template> <transition @enter="enter" ...

Could not locate the express.js file

Currently in the process of learning express/node.js, but struggling to locate the page at localhost:3000/info You can view screenshots of my code below ...

Page jumping vertically in Chrome upon reload, with Firefox and Internet Explorer functioning properly

Utilizing this jQuery script, I am able to center a website vertically within the browser window if it exceeds the height of the outer wrapper-div, which has fixed dimensions. $( document ).ready(function() { centerPage(); )}; // center page vertic ...

Utilize the rapidjson library to convert a JSON array containing a mix of numbers into integer, unsigned integer, and float

From my understanding, this char* represents a valid json-string. const char* jsonData = { "array":[14, -15, 3.17], "array_type": ["uint", "int", "float"] } The requirement is for all numbers in the array to be of 4 bytes length. The question is how one ...

Complete my search input by utilizing ajax

It's only been 30 minutes since my last post, but I feel like I'm making progress with my search posts input: I've developed a model that resembles this: function matchPosts($keyword) { $this->db->get('posts'); ...

How to modify the overlay color in the TouchableHighlight component using an arrow function in React Native

With touchableHighlight, I found that I could easily modify the overlay color using the following code: <TouchableHighlight onPress={this.toggle.bind(this)} underlayColor="#f1f1f1"> However, when attemptin ...