日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 前端文檔 正文

js 實現千分位toLocaleString,獲取當前年份日期范圍

作者:傻的太壞 更新時間: 2024-03-03 前端文檔

1、使用JS內置 API (toLocaleString),實現數字、金額等等數值的格式化

(99999999999999).toLocaleString(); // "99,999,999,999,999" 這種是沒有單位的

// 還可以通過2個參數,進行不同數據的處理,例如人民幣格式、百分比格式
const opts = {
  style: 'currency',
  currency: 'CNY',
};
(99999999999999).toLocaleString('zh-CN', opts); //  ¥99,999,999,999,999.00


const opts = {
  style: 'percent',
  currency: 'CNY',
};
(0.5).toLocaleString('zh-CN', opts); // 50%

2、使用正則

function regexHandleNum(num) {
  return String(num).replace(/\B(?=(\d{3})+(?!\d))/g, ','); // 3是千分位,4是萬分位
}

3、使用js自帶的數組方法進行處理千分位

function handleNum(num) {
  return String(num)
    .split('')
    .reverse()
    .reduce((prev, next, index) => {
      return (index % 3 ? next : next + ',') + prev; // 3是千分位,4是萬分位
    });
}

 // 獲取當前年份日期范圍
            /*************************/
            var currentYear = new Date().getFullYear();
            var start = currentYear + '-01-01';
            var end = currentYear + '-12-31';
            var defaultYear = start + ' - ' + end;
            /*************************/

原文鏈接:https://blog.csdn.net/weixin_38897313/article/details/134227494

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新