Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP ASP.NET ASMX
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 22-Mar-12 20:49pm
Comments
SAKryukov at 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
digimanus at 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.
SAKryukov at 23-Mar-12 11:32am
   
Of course. How "4,4" can represent integer?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Simply use Math.Round function.
 
Function usage:
Math.Round(decimal d,int dec);
According to your Question:
double num = Math.Round(3.44445556, 1);
  Permalink  
v3
Comments
Shahin Khorshidnia at 23-Mar-12 7:05am
   
My +5
Praveen Kullu at 23-Mar-12 7:09am
   
d = 3.44445556 here.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Declare @flt as Float
Set @flt = 3.44445556
Select round(@flt,1)
OUTPUT :3.3999999999999999
 
AND IF YOU USE MONEY DATATYPE 
 
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..
  Permalink  
Comments
Shahin Khorshidnia at 23-Mar-12 7:06am
   
my +5
(__Aaron__) at 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)

  Print Answers RSS
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 23 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100