Hello guys today I'll give you a simple code that'll format the numbers coming out of dataset/collection using the k format or comma format in a repeater or dynamic page
I'm so sorry for breaking the community guidelines by tagging people but during research I found out that @tvalenzuela @Fiona Avalon @gabelec18 @zonaibnajaf needed this code.
things to Know:
$w("#text1").text = where the number should be (don't connect to dataset)
viewCount = the column Id in the collection
$w('#dynamicDataset') = the current item dataset or the current page dataset
$w("#repeater1") = your repeater
Dynamic pages
k format
This will change numbers like "43233" to "43.2k"
This will change numbers like "3232" to "3.2k"
This will change numbers like "893239230" to "893.2M"
This will change numbers like "89323923030" to "1B+"
function Kformat () {
$w('#dynamicDataset').onReady(() => {
let currentItem = $w('#dynamicDataset').getCurrentItem()
let number = currentItem.viewCount
if(number < 1000 && number > -1) {//0 to 999 will be number + nothing
let x = (number/1) + ""
$w("#text1").text = x;
console.log(number);
}
if(number < 1000000 && number > 999) {//999 to 999,999 will be number + k
let x = (number/1000).toFixed(1) + "k";
$w("#text1").text = x;
console.log(x);
}
if(number < 1000000000 && number > 999999) {//999,999 to 999,999,999 will be number + M
let x = (number/1000000).toFixed(1) + "M";
$w("#text1").text = x;
console.log(x);
}
if(number > 999999999) {//number from 1 billion and above will be 1B +
let x = "1B+";
$w("#text1").text = x;
console.log(x);
}
})
}
//do the function on when the dataset is ready
export function dynamicDataset_ready() {
// This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
// Add your code for this event here:
kformat()
}
comma format
This will change numbers like "43233" to "43,233"
This will change numbers like "3232" to "3,232"
This will change numbers like "893239230" to "893,239,230"
This will change numbers like "89323923030" to "89,323,923,030"
function commaFormat (x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
export function dynamicDataset_ready() {
let currentItem= $w('#dynamicDataset').getCurrentItem()
let number = currentItem.viewCount;
let x= commaFormat(number);
$w("#text1").text = x;
}
Repeaters
k format
This will change numbers like "43233" to "43.2k"
This will change numbers like "3232" to "3.2k"
This will change numbers like "893239230" to "893.2M"
This will change numbers like "89323923030" to "1B+"
function Kformat () {
$w("#repeater1").forEachItem( ($item, itemData, index) => {
let currentItem = $item('#dynamicDataset').getCurrentItem()
let number = currentItem.viewCount
if(number < 1000 && number > -1) {//0 to 999 will be number + nothing
let x = (number/1) + ""
$item("#text1").text = x;
console.log(number);
}
if(number < 1000000 && number > 999) {//999 to 1 million will be number + k
let x = (number/1000).toFixed(1) + "k";
$item("#text1").text = x;
console.log(x);
}
if(number < 1000000000 && number > 999999) {//999,999 to 999,999,999 will be number + M
let x = (number/1000000).toFixed(1) + "M";
$item("#text1").text = x;
console.log(x);
}
if(number > 999999999) {//number from 1 billion and above will be 1B +
let x = "1B+";
$item("#text1").text = x;
console.log(x);
}
});
}
export function dynamicDataset_ready() {
kformat()
}
comma format
This will change numbers like "43233" to "43,233"
This will change numbers like "3232" to "3,232"
This will change numbers like "893239230" to "893,239,230"
This will change numbers like "89323923030" to "89,323,923,030"
function commaFormat (x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
export function repeater1_itemReady($item, itemData, index) {
// This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
// Add your code for this event here:
let number= itemData.viewCount;
let x = formatNumber(number);
$item("#text1").text = x;
}
You're welcome
Please know you can also tweak these to your liking
If you have any bugs or errors or maybe even ideas. Please email me at nzubefootballer@gmail.com or you can create a new forum and tag me
DJ bon26
hello! what should i replace the ”views” with? is it the field key or what? i’m new to this..
Thanks for the suggestion
Thank you for your help and the quick response!
Do I just add this code under my current code? I already have nom package. Thank you for your help
@DJ bon26 is there anyway I can add numeric format like numbers like "893239230" to "893.2M" Thank you in advance for any help you can give.
$w.onReady(function () {
let startNum = 14000000000;
let endNum = 14100000000;
const duration = 10; // 1000 macroseconds
$w.onReady(function () {
setInterval(()=> {
countUp();
}, duration);
});
function countUp(){
if (startNum <= endNum ){
$w('#transaction').text = startNum.toLocaleString().toString();
startNum++;
}
}
You'll find all the info on Wix support: https://support.wix.com/en/article/corvid-working-with-npm-packages
Hi DjBon, Thx you for sharing your code with the community. I would like to point you to numeral.js , a npm package that does exactly what you do but with all method ready out of the box, tested and compatible with multiple currencies and number formatting. Here is how you could use it
import numeral from "numeral"; // don't forget to install the npm package export function repeater1_itemReady($item, itemData, index) { let number = itemData.viewCount; // 893239230 $item("#text1").text = "" + numeral(number).format('0,0') // 893,239,230 $item("#text2").text= "" + numeral(number).format('0a') // 893 M }
as you can see in the example, it's really straight forward and does not require custom code. Generally speaking, you don't want to build your own tool but rather bring them from someone who's specialized in building that tool: as a macon, you don't build your bucket, you buy them from a specialized shop. As developer, we must do the same so we can focus on bringing value to our clients :) let me know if you have a question :)