فهرست منبع

modified numberFormat to round down and include decimals

j14-c 3 سال پیش
والد
کامیت
5d8b1723eb

+ 14 - 2
Extensions/UserScript/Return Youtube Dislike.user.js

@@ -137,11 +137,23 @@ function isVideoLoaded() {
   );
 }
 
+function roundDown(num) {
+  if (num < 1000) return num;
+  const decimal = Math.floor(Math.log10(num) - 1);
+  const value = Math.floor(num / 10 ** decimal);
+  return value * (10 ** decimal);
+}
+
 function numberFormat(numberState) {
   const userLocales = navigator.language;
-  const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
 
-  return formatter.format(numberState);
+  const formatter = Intl.NumberFormat(userLocales, {
+    notation: 'compact',
+    minimumFractionDigits: 1,
+    maximumFractionDigits: 1
+  });
+
+  return formatter.format(roundDown(numberState)).replace('.0', '');
 }
 
 function setEventListeners(evt) {

+ 15 - 2
Extensions/chrome/return-youtube-dislike.script.js

@@ -137,10 +137,23 @@
     );
   }
 
+  function roundDown(num) {
+    if (num < 1000) return num;
+    const decimal = Math.floor(Math.log10(num) - 1);
+    const value = Math.floor(num / 10 ** decimal);
+    return value * (10 ** decimal);
+  }
+
   function numberFormat(numberState) {
     const userLocales = navigator.language;
-    const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
-    return formatter.format(numberState);
+
+    const formatter = Intl.NumberFormat(userLocales, {
+      notation: 'compact',
+      minimumFractionDigits: 1,
+      maximumFractionDigits: 1
+    });
+
+    return formatter.format(roundDown(numberState)).replace('.0', '');
   }
 
   var jsInitChecktimer = null;

+ 15 - 2
Extensions/firefox/return-youtube-dislike.script.js

@@ -133,10 +133,23 @@ function isVideoLoaded() {
   );
 }
 
+function roundDown(num) {
+  if (num < 1000) return num;
+  const decimal = Math.floor(Math.log10(num) - 1);
+  const value = Math.floor(num / 10 ** decimal);
+  return value * (10 ** decimal);
+}
+
 function numberFormat(numberState) {
   const userLocales = navigator.language;
-  const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
-  return formatter.format(numberState);
+
+  const formatter = Intl.NumberFormat(userLocales, {
+    notation: 'compact',
+    minimumFractionDigits: 1,
+    maximumFractionDigits: 1
+  });
+
+  return formatter.format(roundDown(numberState)).replace('.0', '');
 }
 
 function setEventListeners(evt) {