Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
typescript code to add ',' for numbers based on thousand
- User
typescript code to add ',' for numbers based on thousand
- Assistant
Here is a function that takes in a number and adds a comma as a thousands separator:
typescriptfunction addCommas(num: number): string { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); }Usage:
typescriptconst number = 1234567; const result = addCommas(number); console.log(result); // "1,234,567" - User
all write comments
- Assistanttypescript
/** * Function to add commas as thousands separator to a number * @param num {number} the number to add commas to * @returns {string} the formatted number with commas as thousands separator */ function addCommas(num: number): string { // Use toString method to convert number to string // Use replace method to add commas as thousands separator // The regular expression searches for non-boundary (\B) occurences of three consecutive digits (\d{3}) // and adds a comma before them, unless they are followed by another digit (!\d) return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.