gpt4 book ai didi

javascript - 平方米计算器帮助四舍五入平方米

转载 作者:行者123 更新时间:2023-11-30 16:47:45 25 4
gpt4 key购买 nike

我正试图帮助一个 friend 修复他的网站,他们请我帮忙整理一个计算平方米的表格计算器……现在它可以准确地计算出来。我的伙伴希望它能将平方米四舍五入。我对代码一窍不通,有没有人可以帮我找到一种将平方米四舍五入的方法?

这是我们目前所拥有的;

<script type="text/javascript">// <![CDATA[

(function($){
jQuery(document).ready(function($){


theUnit = 'cms';
$('#inserted-height').val('');
$('#inserted-width').val('');

$('#inserted-height').focus(function(){
$(this).val('');
});
$('#inserted-width').focus(function(){
$(this).val('');
});

$('#inserted-height').blur(function(){
var height = $(this).val();
if(!checkDigits(height)){
$(this).focus();
$('#calculate-button').removeClass("Reset");
return false;
}
});

$('#inserted-width').blur(function(){
var width = $(this).val();
if(!checkDigits(width)){
$(this).focus();
$('#calculate-button').removeClass("Reset");
return false;
}
});

});

$('#calculate-button').click(function(){

if ($(this).hasClass("Reset")){

$('#screen-two').fadeOut(function(){
$('#cms-message').fadeIn();
});

$(this).removeClass("Reset");
$(this).val("Submit");
$('#inserted-height').val('');
$('#inserted-width').val('');
return false;
}
/* SET SIZES */
var MIN_SIZE = 20000;
var MAX_SIZE = 3000000;

/* FREE SHIPPING */


var currentProductPrice = $('#currentProductPricefromCMS').val();
var estimatedWidth = $('#inserted-width').val();
var estimatedHeight = $('#inserted-height').val();
/* added pre price to calculator */
var currencySymbol = "AUD$";
var totalSize = estimatedWidth * estimatedHeight;
var isInches = false;
// get the unit type
var theUnits = $("input[name='unit']:checked").val()
// console.log(theUnits);

// boolean for valid inputs
var valid = false;

if(theUnit == 'inch') {
isInches = true;
totalSize = (estimatedWidth* 2.54) * (estimatedHeight * 2.54);
} else {
isInches = false;
}
/* WARNING FOR MAX AND MIN SIZES */

if((totalSize < MIN_SIZE || totalSize > MAX_SIZE) && !isInches){
alert('Oops... size must be within '+MIN_SIZE/10000+' sqm and '+MAX_SIZE/10000+' sqm. \n Note: 100 cms = 1 Metre.');
$('#inserted-width').css({'color':' #AA202E'});
$('#inserted-height').css({'color':' #AA202E'});
return false;
} else {

if((totalSize < MIN_SIZE || totalSize > MAX_SIZE) && isInches ){
alert('Oops... size must be within '+MIN_SIZE/10000+' sqm and '+MAX_SIZE/10000+' sqm or approx 27 square feet (MIN) and 323 square feet (MAX). Note: 12 inches = 1 Foot.');
$('#inserted-width').css({'color':'#AA202E'});
$('#inserted-height').css({'color':'#AA202E'});
return false;
} else {
valid = true;
}
}

/* DO IT */
// can only proceed if true
if(valid){
//fade in price

$('#cms-message').fadeOut(function(){
$('#screen-two').fadeIn();
});
$('#calculate-button').val("Reset");
$('#calculate-button').addClass("Reset");

/* IF INCHES */

if(isInches) {
// console.log("inches selected");
var InchesToCms = (totalSize);
var estimatedPrice = InchesToCms * currentProductPrice;
// console.log("inches to cms"+totalSize);
var outputPrice = estimatedPrice / 10000;
var actualPrice = outputPrice.toFixed(2);
$('#display-price').val(currencySymbol+actualPrice);
}
/* IF CM */
else {
// console.log("cms selected");
var estimatedPrice = totalSize * currentProductPrice;
/* added for rounding up */
var totalSize = Math.round(outputPrice);
var outputPrice = estimatedPrice / 10000;

var actualPrice = outputPrice.toFixed(2);


$('.symbol').html("<span>" + currencySymbol + "</span >");
$('#display-price').val(actualPrice);
}
var free_shipping_min = 330;
/* check if above $400 for actual price */
if (actualPrice >= free_shipping_min ){
$(".calculator_disclaimer").html('<b>Price includes <span>FREE</span> shipping</b>');

} else {

$(".calculator_disclaimer").html("Price excludes shipping $49.95");
}

}

});

//REGEX FOR Checking if letters entered.
function checkDigits(str){

var intRegex = /[A-z]/;
if(intRegex.test(str) ) {
alert('Please enter only numbers');
return false;
} else {
return true;
}

}


/* RESET BUTTON */
$('.reset').click(function($){
totalSize = 0;
$('#inserted-width').css({'color':'solid 1px #969696'});
$('#inserted-height').css({'border':'solid 1px #969696'});

$('#inserted-height').val('');
$('#inserted-width').val('');
});

/* TOGGLE BETWEEN INCHES AND CMS */

$('#radio-inches').click(function($){
$('.units').val('Inches');
theUnit = 'inch';
});

$('#radio-cms').click(function(){
$('.units').val('Cms');
theUnit = 'cms';
});

})(jQuery);
// ]]></script>

好的,这就是我按照建议进行更改后的结果。

<div class="quick-quote">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<div class="home_price_calculator" style="text-align: center;">
<h3 style="text-align: center;">Quick Quote</h3>
Please enter your wall size in centimetres. <!-- HIDDEN FIELDS GETS THE CURRENT PRICE PER SQUARE METRE --> <input id="currentProductPricefromCMS" type="hidden" autocomplete="off" value="55.000" /> <input id="inserted-width" class="validate" type="text" autocomplete="off" placeholder="length" size="9" value="" /> X <input id="inserted-height" class="validate" type="text" autocomplete="off" placeholder="height" size="9" value="" /> <input id="calculate-button" class="calculate" type="button" value="Submit" name="calculate" /> <!-- <div id="cms-message">Please enter your size in centimetres.</div> --> <input id="display-currency" type="hidden" value="" />$AUD <input id="display-price" type="text" readonly="readonly" size="9" />
<div class="calculator_disclaimer" style="text-align: center;">&nbsp;</div>
</div>
</div>
<script type="text/javascript">// <![CDATA[
/* SETS CENTIMETRES ONLOAD */
(function($){
jQuery(document).ready(function($){

/* EXECUTION OF PRICING ESTIMATOR */
theUnit = 'cms';
$('#inserted-height').val('');
$('#inserted-width').val('');

$('#inserted-height').focus(function(){
$(this).val('');
});
$('#inserted-width').focus(function(){
$(this).val('');
});

$('#inserted-height').blur(function(){
var height = $(this).val();
if(!checkDigits(height)){
$(this).focus();
$('#calculate-button').removeClass("Reset");
return false;
}
});

$('#inserted-width').blur(function(){
var width = $(this).val();
if(!checkDigits(width)){
$(this).focus();
$('#calculate-button').removeClass("Reset");
return false;
}
});

});

$('#calculate-button').click(function(){

if ($(this).hasClass("Reset")){

$('#screen-two').fadeOut(function(){
$('#cms-message').fadeIn();
});

$(this).removeClass("Reset");
$(this).val("Submit");
$('#inserted-height').val('');
$('#inserted-width').val('');
return false;
}
/* GETS INITIAL SIZES */
var MIN_SIZE = 20000;
var MAX_SIZE = 3000000;

/* FREE MIN SHIPPING VARIABLE */


var currentProductPrice = $('#currentProductPricefromCMS').val();
var estimatedWidth = $('#inserted-width').val();
var estimatedHeight = $('#inserted-height').val();
/* added pre price to calculator */
var currencySymbol = "AUD$";
/* next line as per advice */
var totalSize = Math.ceil((estimatedWidth * estimatedHeight));
var isInches = false;
// get the unit type
var theUnits = $("input[name='unit']:checked").val()
// console.log(theUnits);

// boolean for valid inputs
var valid = false;

if(theUnit == 'inch') {
isInches = true;
/* next line as per advice */
totalSize = Math.ceil((estimatedWidth * 0.0254) * (estimatedHeight * 0.0254));
} else {
isInches = false;
}
/* WARNING FOR MAX AND MIN SIZES */

if((totalSize < MIN_SIZE || totalSize > MAX_SIZE) && !isInches){
alert('Oops... size must be within '+MIN_SIZE/10000+' sqm and '+MAX_SIZE/10000+' sqm. \n Note: 100 cms = 1 Metre.');
$('#inserted-width').css({'color':' #AA202E'});
$('#inserted-height').css({'color':' #AA202E'});
return false;
} else {

if((totalSize < MIN_SIZE || totalSize > MAX_SIZE) && isInches ){
alert('Oops... size must be within '+MIN_SIZE/10000+' sqm and '+MAX_SIZE/10000+' sqm or approx 27 square feet (MIN) and 323 square feet (MAX). Note: 12 inches = 1 Foot.');
$('#inserted-width').css({'color':'#AA202E'});
$('#inserted-height').css({'color':'#AA202E'});
return false;
} else {
valid = true;
}
}

/* DO IT */
// can only proceed if true
if(valid){
//fade in price

$('#cms-message').fadeOut(function(){
$('#screen-two').fadeIn();
});
$('#calculate-button').val("Reset");
$('#calculate-button').addClass("Reset");

/* IF INCHES SELECTED */

if(isInches) {
// console.log("inches selected");
var InchesToCms = (totalSize);
var estimatedPrice = InchesToCms * currentProductPrice;
// console.log("inches to cms"+totalSize);
var outputPrice = estimatedPrice / 10000;
var actualPrice = outputPrice.toFixed(2);
$('#display-price').val(currencySymbol+actualPrice);
}
/* IF CM SELECTED */
else {
// console.log("cms selected");
var estimatedPrice = Math.round((totalSize) * (currentProductPrice));
var outputPrice = estimatedPrice / 10000;

var actualPrice = outputPrice.toFixed(2);
/* added for rounding up */
var outputPrice = Math.round(outputPrice);

$('.symbol').html("<span>" + currencySymbol + "</span >");
$('#display-price').val(actualPrice);
}
var free_shipping_min = 330;
/* check if above $400 for actual price */
if (actualPrice >= free_shipping_min ){
$(".calculator_disclaimer").html('<b>Price includes <span>FREE</span> shipping</b>');

} else {

$(".calculator_disclaimer").html("Price excludes shipping $49.95");
}

}

});

//REGEX FOR Checking if letters entered.
function checkDigits(str){

var intRegex = /[A-z]/;
if(intRegex.test(str) ) {
alert('Please enter only numbers');
return false;
} else {
return true;
}

}


/* RESET BUTTON */
$('.reset').click(function($){
totalSize = 0;
$('#inserted-width').css({'color':'solid 1px #969696'});
$('#inserted-height').css({'border':'solid 1px #969696'});
/* added by coops 1 Feb 2013 */
$('#inserted-height').val('');
$('#inserted-width').val('');
});

/* TOGGLE BETWEEN INCHES AND CMS */

$('#radio-inches').click(function($){
$('.units').val('Inches');
theUnit = 'inch';
});

$('#radio-cms').click(function(){
$('.units').val('Cms');
theUnit = 'cms';
});

})(jQuery);
// ]]></script>
<div class="bottom_tab">&nbsp;</div>
<div class="bottom_tab" style="text-align: center;">&nbsp;</div>

最佳答案

鉴于您缺乏经验,我建议您首先参加 JS 教程。不过,我很钦佩这种自信!

请注意,您可以使用单词 ceil 或单词 round:ceil 会将 1.01、1.5 等四舍五入到 2,round 将四舍五入 1.01、1.49 等降至 1 和 1.5、1.99 等,直至 2。


改变行:

var totalSize = estimatedWidth * estimatedHeight;

到:

var totalSize = Math.round((estimatedWidth / 100) * (estimatedHeight / 100));

或:

var totalSize = Math.ceil((estimatedWidth / 100) * (estimatedHeight / 100));

还有一行:

totalSize = (estimatedWidth* 2.54) * (estimatedHeight * 2.54);

到:

totalSize = Math.round((estimatedWidth * 0.0254) * (estimatedHeight * 0.0254));

或:

totalSize = Math.ceil((estimatedWidth * 0.0254) * (estimatedHeight * 0.0254));

关于javascript - 平方米计算器帮助四舍五入平方米,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30977014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com