Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I enter the number 3.4 on the form in C # windows application into a database sql server 2005,, a number that is printed is 3.44445556.

how to change the number 3.44445556 to 3.4?
Posted
Comments
Sergey Alexandrovich Kryukov 23-Mar-12 2:23am    
Sounds strange... Float accuracy is low, but not that bad... :-)
Did you run it all under debugger, check the value immediately before getting a string representation? Could be some bug. By the way, float is used rarely, as the accuracy is often not enough, usually people use double... All System.Math is written in double only.
--SA
[no name] 23-Mar-12 3:55am    
ok! thanks
after using a variable double in c# can be success, but on asp.net web service can not and will display a warning:

System.ArgumentException: Cannot convert 4,4 to System.Int32.
Parameter name: type ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
--- End of inner exception stack trace ---
at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

what's with double variables on a web service?
Herman<T>.Instance 23-Mar-12 4:26am    
better show the code that creates the error

since the value is 4,4 in stead of 4.4 the error is found easily. You have to use globalization to prevent this. And you try to convert ToInt32() in stead of ToFloat()
and read this.
[no name] 23-Mar-12 4:52am    
thanks very much, I will learn it
Sergey Alexandrovich Kryukov 23-Mar-12 11:32am    
Of course. How "4,4" can represent integer?
--SA

Simply use Math.Round function.

Function usage:
C#
Math.Round(decimal d,int dec);

According to your Question:
C#
double num = Math.Round(3.44445556, 1);
 
Share this answer
 
v3
Comments
[no name] 23-Mar-12 6:47am    
thanks very much Mr. Kullu
[no name] 23-Mar-12 6:56am    
what is the value of "d" in your program?
Shahin Khorshidnia 23-Mar-12 7:05am    
My +5
Praveen Kullu 23-Mar-12 7:09am    
d = 3.44445556 here.
SQL
Declare @flt as Float
Set @flt = 3.44445556
Select round(@flt,1)

OUTPUT :3.3999999999999999

HTML
AND IF YOU USE MONEY DATATYPE 


SQL
Declare @flt as MONEY
Set @flt = 3.44445556
Select round(@flt,1)


OUTPUT : 3.40


BETTER TO USE MONEY DATATYPE


Hope This May Help You..
 
Share this answer
 
Comments
[no name] 23-Mar-12 6:59am    
thanks a lot of Mr. Ajith
Shahin Khorshidnia 23-Mar-12 7:06am    
my +5
(__Aaron__) 24-Mar-12 2:06am    
nice!

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