Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How will we accept the decimal numbers and how will round off the value if it is more than .5
Posted
Comments
Sinisa Hajnal 2-Sep-14 2:25am    
Please define your question better. Accept from where, text box, database, some class? Rounding you can do with Math.Round function.

All of this could be very easily googled. Please do not post questions before doing some research.
Sergey Alexandrovich Kryukov 2-Sep-14 2:38am    
Probably you don't mean rounding but want to limit the value by some maximum (and minimum). Rounding is rarely (almost never) needed.
You need to define the problem much better.
—SA

I assume you mean the user enters a decimal value in a textbox and you convert it to a numeric value:
C#
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[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Sep-14 2:40am    
Do you really believe that OP meant rounding? Do you really believe that rounding could be ever useful or even make sense?
(I don't mean rare use of rounding, such as in random number generation algorithms, I mean rounding off input data. Actually, this would be a disaster.)
—SA
OriginalGriff 2-Sep-14 3:26am    
Yes.
And yes.

You (and I) have no idea of what his application is doing: the rounding may well make sense in context. To assume it doesn't is...well, rather arrogant, don't you think?
Sergey Alexandrovich Kryukov 2-Sep-14 11:26am    
No, why? I would first ask a question and see what OP really wants. You see, rounding is one of the usual mistakes. Its better to try to address it. So far, I saw many questions on rounding, and after some discussion, all of those questions were based on some misconception; and rounding was not really needed. (I don't mean rounding which happens under the hood when you format a number.)
—SA
Hi,
Try this code :

ASP.NET
 <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-9]*[1-9]+[0-9]*$)">
</asp:regularexpressionvalidator>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900