Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7660149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:25:36+00:00 2026-05-31T13:25:36+00:00

With this code there’s the possibility that the user would enter either a 0

  • 0

With this code there’s the possibility that the user would enter either a 0 or nothing at all in the ‘numeric’ form fields.
Is there a ‘catch all’ so that if the user does this the NaN or resulting 0.0000% is just replaced with 0?
I think this needs two logics(?) one for the NaN replacement and one to change the .appendTo result so that it only shows 0 rather than 0.00000%

here’s a JSFiddle – http://jsfiddle.net/sturobson/xTEKm/39/

and here’s the jquery –

$(document).ready(function() {

$(function(){
$("#result").submit(function(e) {
    e.preventDefault();

    var ele = $("#element").val(),
        target = $("#target").val(),
        context = $("#context").val(),
        border = $("#border").val(),
        margin = $("#margin").val(),
        padding = $("#padding").val();

    console.log(ele, target, context, border, margin, padding);

    var DoubleMargin = parseInt(margin, 10) * 2;
    var DoublePadding = parseInt(padding, 10) * 2;
    var DoubleBorder = parseInt(border, 10) * 2;


    var ActualTarget = parseInt(target, 10) - parseInt(DoubleBorder, 10) - parseInt(DoubleMargin, 10) - parseInt(DoublePadding, 10) * 1;
    var result3 = parseInt(target, 10) - parseInt(DoubleMargin, 10) * 1;
    var MarginResult = ((parseInt(margin, 10) / parseInt(target, 10)) * 100).toFixed(5);
    var PaddingResult = ((parseInt(padding, 10) / parseInt(target, 10)) * 100).toFixed(5);
    var OriginalResult = ((parseInt(ActualTarget, 10) / parseInt(context, 10)) * 100).toFixed(5);


    var BorderResult = parseInt(target, 10) - parseInt(border, 10) * 1;


    //$(".result").append(ele + " " + result + "%");
    $("<p></p>", {
        html: ele + " {<br><span>width: " + OriginalResult + "%;" + " /* " + ActualTarget + " (originally " + target + ") / " + context + " */ " + "<br>border: " + border + "px; " + "<br>margin: " + MarginResult + "%; " + "<br>padding: " + PaddingResult+ "%;" + "<br> </span>}"
    }).hide().appendTo("#code-results").fadeIn();
});

}); 

});

any ideas?​

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:25:37+00:00Added an answer on May 31, 2026 at 1:25 pm

    Convert once at the getter:

    var ele = $("#element").val(),
        target = (isNaN(target = parseInt($("#target").val(), 10)) ? 0 : target),
        context = (isNaN(context = parseInt($("#context").val(), 10)) ? 0 : context),
        border = (isNaN(border = parseInt($("#border").val(), 10)) ? 0 : border),
        margin = (isNaN(margin = parseInt($("#margin").val(), 10)) ? 0 : margin),
        padding = (isNaN(padding = parseInt($("#padding").val(), 10)) ? 0 : padding);
    

    Also see your updated example.

    === UPDATE ===

    With toFixed(x) you get exactly x decimals. If you want to get up to max x decimals, you can use following function instead:

    function round(fValue, iDecimals) {
        var iPow = Math.pow(10, iDecimals);
        return Math.round(fValue * iPow) / iPow;
    }
    

    Also see your updated example.

    === UPDATE ===

    The next problem is, that a denominator of a fraction has to be a non-zero value. You can add a division function which returns zero instead of NaN, if the denominator is zero:

    function div(fNumerator, fDenominator) {
        return (fDenominator == 0 ? 0 : fNumerator / fDenominator);
    }
    

    Also see your updated example.

    === UPDATE ===

    If you want the code a little smaler, you can combine it with T.J. Crowder answer; the source code could be:

    function round(fValue, iDecimals) {
        var iPow = Math.pow(10, iDecimals);
        return Math.round(fValue * iPow) / iPow;
    }
    
    function div(fNumerator, fDenominator) {
        return (fDenominator == 0 ? 0 : fNumerator / fDenominator);
    }
    
    $(document).ready(function() {
    
        $(function(){
            $("#result").submit(function(e) {
                e.preventDefault();
    
                var ele = $("#element").val(),
                    target = parseInt($("#target").val(), 10) || 0,
                    context = parseInt($("#context").val(), 10) || 0,
                    border = parseInt($("#border").val(), 10) || 0,
                    margin = parseInt($("#margin").val(), 10) || 0,
                    padding = parseInt($("#padding").val(), 10) || 0;
    
                console.log(ele, target, context, border, margin, padding);
    
                var DoubleMargin = margin * 2;
                var DoublePadding = padding * 2;
                var DoubleBorder = border * 2;
    
    
                var ActualTarget = target - DoubleBorder - DoubleMargin - DoublePadding * 1;
                var result3 = target - DoubleMargin * 1;
                var MarginResult = round(div(margin, target) * 100, 5);
                var PaddingResult = round(div(padding, target) * 100, 5);
                var OriginalResult = round(div(ActualTarget, context) * 100, 5);
    
    
                var BorderResult = target - border * 1;
    
    
                //$(".result").append(ele + " " + result + "%");
                $("<p></p>", {
                    html: ele + " {<br><span>width: " + OriginalResult + "%;" + " /* " + ActualTarget + " (originally " + target + ") / " + context + " */ " + "<br>border: " + border + "px; " + "<br>margin: " + MarginResult + "%; " + "<br>padding: " + PaddingResult+ "%;" + "<br> </span>}"
                }).hide().appendTo("#code-results").fadeIn();
            });
    
        });
    
    });
    

    Also see your updated example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trouble finding way to correctly refactor this code so that there would
Is there an easy way to modify this code so that the target URL
Is there any way to refactor this code so that it can omit unnecessary
If there is any possibility to make this code simpler, I'd really appreciate it!
I wrote this code and there is a problem that I can't seem to
Is there anyway this code can be refactored? The only difference is the order
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
Is there a better way to write this code? I want to show a
Is there a way to make this code work? LogonControl.java @Audit(AuditType.LOGON) public void login(String
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.