PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 9, 2022

[FIXED] How to limit my decimal value to 2 places on my <td>

 August 09, 2022     decimal, html-table, javascript, jquery     No comments   

Issue

I'm making a query to my database to return a value of double precision. This value in my database goes up to 17 decimal places but I only want to display 2 decimal places on the web page.

This is my html with the table. The value of the decimal goes into the {{savings_percent}}

 <table class="table text-light text-end">
                                        <thead>
                                            <tr>
                                                
                                                <th scope="col">CHW</th>
                                       
                                            </tr>
                                        </thead>
                                        <tbody class="text-end">
                                            <tr id="decimal">
                                                <th scope="row">%</th>
                                                {{#with chw}}
                                                <td class='rounded'>{{savings_percent}}</td>
                                                {{/with}}
                                            
                                        </tbody>
                                    </table>

This is how I'm trying to grab the value, convert it to decimal and then give it a fixed number of two decimals. The console is giving me $(...).val(...).toFixed is not a function. Does anyone have a solution on how I can fix this?

let decimal = parseFloat($("#decimal .rounded").text())
    
    window.onload = () => {

        $('.rounded').val(decimal).toFixed(2)

        console.log(decimal)   
    };

Solution

val() is only for form controls

I would use text(function) which gives you the current text to do what you need to before returning modifications

$('.rounded').text(function(i,curr){
    return parseFloat(curr).toFixed(2)
})


Answered By - charlietfl
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing