JSFiddle - React, Tailwind, and code Playground

by daraii

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div>
  <h2>
    
  </h2>
</div>

JavaScript

var actualCases = "120.246";
var out1 = Number(actualCases.replace(/[^\d\-\.]/g, '')).toLocaleString('en', {minimumFractionDigits: 0});
console.log(out1);

var out3 = Math.round((getCalcVal(actualCases) + Number.EPSILON) * 100) / 100;
console.log(out3);

function fmtdNum(num, fractionalDigits, maxFlag) {
    if (fractionalDigits == undefined) {
        fractionalDigits = 2;
    }
    var optName = "maximumFractionDigits";

    if (maxFlag == undefined) {
        optName = "minimumFractionDigits";
    } else {
        if (maxFlag != true) {
            optName = "minimumFractionDigits";
        }
    }

    var options = {};
    options[optName] = fractionalDigits;
    var formattedNum = Number(num).toLocaleString('en', options);

    return formattedNum;
}

function getCalcVal(obj, fixedDecimals) {
    var objVal;

    if (obj == undefined) {
        objVal = null;
    } else {
        try {
            //Treat obj as a jQuery object
            if ($(obj).length > 0 && $(obj).is('td')) {
                objVal = $(obj).text().replace(/,|\$|%/g, '');
            } else {
                // Chrome doesn't throw an exception if it's not a jQuery selector.
                //Try to get the selector value
                if ($(obj).length > 0) {
                    objVal = $(obj).val().replace(/,|\$|%/g, '');
                } else if (typeof obj == "string") {
                    objVal = obj.length > 0 ? obj.replace(/,|\$|%/g, '') : null;
                }
            }
        } catch (err) {
            //It failed to be treated as an jQuery object (Firefox). Test if it is a string.
            if (typeof obj == "string") {
                console.log("It is a string");
                objVal = obj.length > 0 ? obj.replace(/,|\$|%/g, '') : null;
            } else {
                console.log(err);
            }
        }
    }

    objVal = (objVal !== undefined && objVal !== null) ? objVal.trim() : objVal;
    objVal = objVal ? objVal : "0";

    var parsed =...