Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to use "parse Float" in JavaScript

odiez

Coder
Hi

I have a table and I got it to sort my table headers, but I have a few columns with 100 diffrent numbers, and it is not sorting correct. I did look at parse float, but cannot figure it out.
How do you use it if numbers are like this. 0.0000000000, 12.00, 12,999,999,999,999,999.01234567. this is just example.
Di I have to change table info also.
Here is my script that I have
JavaScript:
/**
 * Sorts a HTML table.
 *
 * @param {HTMLTableElement} table The table to sort
 * @param {number} column The index of the column to sort
 * @param {boolean} asc Determines if the sorting will be in ascending
 */
 function sortTableByColumn(table, column, asc = true) {
    const dirModifier = asc ? 1 : -1;
    const tBody = table.tBodies[0];
    const rows = Array.from(tBody.querySelectorAll("tr"));

    // Sort each row
    const sortedRows = rows.sort((a, b) => {
        const aColText = a.querySelector(`td:nth-child(${ column + 1 })`).textContent.trim();
        const bColText = b.querySelector(`td:nth-child(${ column + 1 })`).textContent.trim();

        return aColText > bColText ? (1 * dirModifier) : (-1 * dirModifier);
    });

    // Remove all existing TRs from the table
    while (tBody.firstChild) {
        tBody.removeChild(tBody.firstChild);
    }

    // Re-add the newly sorted rows
    tBody.append(...sortedRows);

    // Remember how the column is currently sorted
    table.querySelectorAll("th").forEach(th => th.classList.remove("th-sort-asc", "th-sort-desc"));
    table.querySelector(`th:nth-child(${ column + 1})`).classList.toggle("th-sort-asc", asc);
    table.querySelector(`th:nth-child(${ column + 1})`).classList.toggle("th-sort-desc", !asc);
}

document.querySelectorAll(".table-sortable th").forEach(headerCell => {
    headerCell.addEventListener("click", () => {
        const tableElement = headerCell.parentElement.parentElement.parentElement;
        const headerIndex = Array.prototype.indexOf.call(headerCell.parentElement.children, headerCell);
        const currentIsAscending = headerCell.classList.contains("th-sort-asc");

        sortTableByColumn(tableElement, headerIndex, !currentIsAscending);
    });
});

Here is my th of my table

Code:
 <table class="table table-sortable">
             <thead>
                    <tr>
                        <th height="50">Name</th>
                        <th height="50">WWW</th>
                        <th height="50">CHAIN</th>
                        <th height="50">Value</th>
                        <th height="50">Day</th>
                        <th height="50">Week</th>
                        <th height="50">Month</th>
                        <th height="50">Year</th>
                        <th height="50">Coins</th>
                        <th height="50">Button</th>
                        <th height="50">USD</th>
                        <th height="50">Button</th>
                        <th height="50">ZAR</th>
                    </tr>
            </thead>
            <tbody>
                    <tr>
 
First: You can not use commas!!! They are not numbers.
This is how to use:
Code:
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML =
    parseFloat( 0.0000000000) + "<br>" +
    parseFloat( 12.00) + "<br>" +
    parseFloat(112,000) + "<br>" +
    parseFloat("112999999999999999012345670");
    </script>
 
First: You can not use commas!!! They are not numbers.
This is how to use:
Code:
    <p id="demo"></p>
   
    <script>
    document.getElementById("demo").innerHTML =
    parseFloat( 0.0000000000) + "<br>" +
    parseFloat( 12.00) + "<br>" +
    parseFloat(112,000) + "<br>" +
    parseFloat("112999999999999999012345670");
    </script>
Thank you, but the numbers change every few seconds, I do not think parse float is going to work, do you know what else can work for this.
I using this for cryptocurrency price
 
I
What are you trying to see? The highest rate of exchange? the best number of buyers?
And where do these numbers come from? How do you get them?
I am getting all of this info (Price, the 24h, 7day,30day and 365 day % of each token), from nomicsdotcom, it is all working, but the numbers of USD and ZAR must only show 2 digests after the . eg. (12.00) and they show 12.123456
 
Well, not everything I asked and a new question. Use toFixed() to solve your question: Ex.
let num = 12.3456789;
let n = num.toFixed(2);
You can do this as you add to the table.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom