【问题标题】:How to hide numbers after dot in float value in input using jquery如何使用jquery在输入中的浮点值中隐藏点后的数字
【发布时间】:2015-12-09 05:49:53
【问题描述】:

我正在开发一个金融计算器,如果我将值四舍五入,我将面临获得准确答案的问题。基本上我不想在浮点值中显示点后的数字,但我在计算中需要相同的浮点值。 注意:- 计算太多,很难将值保存在变量中

例如

value1 = Math.round(34.55); // it will be 35
$('#input1').val(value1); // and it is fine to show round value which is 35
value2 = ("#input1").val(); // In this line it will be 35 but i need 34.55

【问题讨论】:

  • 34.55 是从哪里来的?使用该直接价值。
  • 您可以将 34.55 值保存在 var 或隐藏的 html 字段中。将值分配给 value2 时使用该隐藏值
  • 使用 jQuery .data,保存实际值,使用.val 显示四舍五入值。例如$('#input1').data('actual_value',34.55).val(Math.round(34.55));
  • 使用data属性存储真实值,value属性为用户看到的四舍五入的值
  • $('#input1').data('actual_value');获取实际值

标签: javascript jquery html numbers


【解决方案1】:

试试这个

var floatVal = 34.55;
value1 = Math.round(floatVal); // it will be 35 
$('#input1').attr("data-floatVal","34.55");
$('#input1').val(value1);
//it's important to parseFloat
value2 = parseFloat($("#input1).attr("data-floatVal")); 

【讨论】:

  • 使用$.data() - > $('#input1').data("actualVal","34.55"); 而不是$('#input1').attr("data-actualVal","34.55");
【解决方案2】:

将其设置为该输入元素的属性

var decimalValue = 34.55;
$('#input1').attr( "data-value", decimalValue );
$('#input1').val( Math.round( decimalValue ) ); 
value2 = ("#input1).val(); //decimal value
actualValue2 = ("#input1).attr( "data-value" ); // float value

【讨论】:

    【解决方案3】:

    试试这个 //我希望这对你有用。 roundValue = Math.round(34.55); actualValue=34.55;//创建另一个变量来存储实际值并在需要时使用。 $('#input1').val(roundValue);

    值2 = 实际值;

    try this //I hope this will work for you.
     roundValue = Math.round(34.55); 
     actualValue=34.55;//Create another variable to store actual value and use when required.
    $('#input1').val(roundValue);
    
    value2 = actualValue;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 2011-10-13
      • 2018-06-21
      相关资源
      最近更新 更多