我们如何接受十进制数字,如果它超过0.5,将如何舍入该值 [英] How will we accept the decimal numbers and how will round off the value if it is more than .5

查看:66
本文介绍了我们如何接受十进制数字,如果它超过0.5,将如何舍入该值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何接受十进制数字以及如果它超过.5

解决方案

我将假设您的意思是用户输入小数文本框中的值,并将其转换为数值:

  decimal  val; 
if (!decimal.TryParse(myTextBox.Text, out val))
{
// 向用户报告错误
返回;
}



舍入也很简单:您可以使用许多重载来获得您想要的结果:Math.Round [ ^ ]


您好,

试一试代码:



 <   script     language   =  javascript >  
function mathRoundForDecimals(source){
var txtBox = document .getElementById(source);
var txt = txtBox.value;
if (!isNaN(txt)&& isFinite (txt)&& ; txt.length!= 0 ){
var rounded = 数学 .round(TXT);
alert(四舍五入);
txtBox.value = rounded.toFixed( 2 );
}
}

< / < span class =code-leadattribute> script
>


< asp:textbox id = txtDecimal runat = server onkeypress = return isNumberKey(this,event); onchange = mathRoundForDecimals(this.id); xmlns:asp = #unknown > < / asp:textbox >

< asp:regularexpressionvalidator xmlns:asp < span class =code-keyword> = #unknown >
id =RegularExpressionValidator1
runat =server
ControlToValidate =txtDecimal
ErrorMessage =Numeric and Desimal
ValidationExpression =(^ [0-9] * [1-9] + [0-9] * \ [0-9] *

)|。。(^ [0-9] * \ [0-9 ] * [1-9] + [0-9] *

How will we accept the decimal numbers and how will round off the value if it is more than .5

解决方案

I assume you mean the user enters a decimal value in a textbox and you convert it to a numeric value:

decimal val;
if (!decimal.TryParse(myTextBox.Text, out val))
   {
   // Report error to user
   return;
   }


Rounding is also simple: there are a number of overloads you can use to get exactly the result you want: Math.Round[^]


Hi,
Try this code :

 <script language="javascript">
 function mathRoundForDecimals(source) {
         var txtBox = document.getElementById(source);
         var txt = txtBox.value;
         if (!isNaN(txt) && isFinite(txt) && txt.length != 0) {
             var rounded = Math.round(txt);
             alert(rounded);
             txtBox.value = rounded.toFixed(2);
         }
     }

    </script>


   <asp:textbox id="txtDecimal" runat="server" onkeypress="return isNumberKey(this, event);" onchange="mathRoundForDecimals(this.id);" xmlns:asp="#unknown"></asp:textbox>

<asp:regularexpressionvalidator xmlns:asp="#unknown">
  id="RegularExpressionValidator1" 
  runat="server" 
  ControlToValidate="txtDecimal" 
  ErrorMessage="Numeric and Desimal" 
  ValidationExpression="(^[0-9]*[1-9]+[0-9]*\.[0-9]*


)|(^[0-9]*\.[0-9]*[1-9]+[0-9]*


这篇关于我们如何接受十进制数字,如果它超过0.5,将如何舍入该值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆