.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Convert/Cast Single to double inaccurate.

Posted By:      Posted Date: April 10, 2011    Points: 0   Category :.NET Framework

Whilst I have read some background on floating opint numbers not being able to accurately store numbers such as 0.1, I am not clear why converting from single to double is so inaccurate.

Converting a single 0.1 to a double 0.1 yeilds


But 0.1 can be represented in a double more accurately, even if not perfectly as


This stems from the single representation of 0.1 actually equating to 0.10000000149011600 in binary.

However, we know that the single has a particular level of precision.

I would like a conersion function that converts single 0.10000000 to double 0.1000000000000000.

I can appreciate that the standard conversion functions would need to still exist as real numbers for engineering would suffer less overall inaccuracies with the existing rounding functions.


One other question:

Can the convertsion be affected by the OS/framework/hardware or will it be consistent on different machines?  Are there any settings that can change the conversion behaviour?



View Complete Post

More Related Resource Links

how to convert into a single SELECT

I need a query in single "SELECT" and capture all my 3 values into 3 different variables..please help. DECLARE @SourceSystem VARCHAR(100) DECLARE @TransactionType VARCHAR(100) DECLARE @TransactionVersion VARCHAR(100) DECLARE @StrXML XML SET @StrXML = '<PDI_Approval_Message> <Header> <SourceSystem Value="PDI/Workforce" Name="WF"></SourceSystem> <TransactionType Value="Timesheet" Name="WF TS"></TransactionType> <TransactionVersion Value="1.0.0" Name=""></TransactionVersion> <RequestType Value="Submit" Name="Action"></RequestType> <SubmittedOn Value="02/06/2010" Name=""></SubmittedOn> <SubmittedBy Value="AYAZ" Name="hannok"></SubmittedBy> </Header> </PDI_Approval_Message>' SELECT @SourceSystem = x.y.query('data(@Value)').value('.', 'VARCHAR(100)') FROM @StrXML.nodes('//PDI_Approval_Message/Header/SourceSystem') AS x(y) SELECT @TransactionType = x.y.query('data(@Value)').value('.', 'VARCHAR(100)') FROM @StrXML.nodes('//PDI_Approval_Message/Header/TransactionType') AS x(y) SELECT @TransactionVersion = x.y.query('data(@Value)').value('.', 'VARCHAR(100)&#

cast and convert issues? really puzzled!

Hi all,   I have an issue with cast, when i am converting nvarchar which is a number to float or money, i get the result multiplied by 100 in some cases and in some cases it is fine. Ex: select cast ( [qty] as float), cast([price] as money) qty and price are both nvarchar(50) if qty - 10.00 price 200.00 (before conversion),  its qty - 1000 and price is 20000 (after conversion)   Please let me know if there is any issue using cast.   Thanks in advance.

How to convert string to double

Hello All!! Since I am pretty new to C sharp I am unable to solve simple conversion from string to double. Example would be like this: string sVal = "-7.0"; //This is negative number. double dVal = 0.0; Now I need to assign dVal with value from sVal and I did try those examples but none of them worked. dVal = double.parse(sval); nor it worked this way dVal = System.Convert.ToDouble(sVal); Please advise what am I doing wrong and how should I solve this pretty simple problem. Thanks in advance.

decimal place problem in convert string to double


Hi guys, i have a strange error creaping into my web app but am unsure as to how to fix it.  When i run this code on my debug workstation it works fine but when i transfer it to my server things seam to go wrong.

dev station (windows 7) + VS2010
regional and language settings all set to uk
server (windows 2008r2)
regional and language settings all set to uk

What seams to be happening is that when converting a string to a double it works correctly on the dev environment but moves the decimal place to the end on the server.

example code(vb)

dim lat as string = "52.983829"
dim long as string = "-0.036500"

dim latint as double = lat
dim longint as double = long

now on the dev server the value of latint is 52.983829 as you might expect but on when published to the server it becomes 52983829.0

the same with longint on the dev staion it is -0.036500 but on the server it becomes -36500.0

i get equally confusing problems if i try to change it to an integer instead.


can anyone help point be in the right direction?

Convert IList to datatable Error "Only a single enumeration is supported by this IEnumerable."



I am working in WCF Data service, in client side i am returning result as IEnumerable as below.

IEnumerable<MTSR> SearchResult = context1.Execute<MTSR>(new Uri(url));

i am converting this enumerable to datatable as below.

public static DataTable ToDataTable<T>(IList<T> data)

            PropertyDescriptorCollection props =
            DataTable table = new DataTable();
            for (int i = 0; i < props.Count; i++)
                PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name, prop.PropertyType);
            object[] values = new object[props

convert double to string?



i've got this error with this code...?:

Cannot implicitly convert type 'double' to 'string'

   if (Convert.ToDouble(Label1.Text) >= Convert.ToDouble(Label55.Text))
Line 147: {
Line 148: Label1.Text = ((Convert.ToDouble(Label1.Text) - Convert.ToDouble("0.5")));

Cast Convert Question


What is the "best practice" for converting from one custom reference type to another? I typically handle this in home grown "Converter" classes and was just looking for a more patterened approach.

Chris Wigley MCAD\MCPD

Application Architecture in Windows Forms 2.0-Single-MDI Applications

Consider an MDI application like Microsoft Excel; files opened from the file system (by double-clicking) are all opened as separate child windows within the parent Excel window.7 For the first instance of an MDI application to open a new child window to display the file that was passed to the second instance of the application, the second instance must be able to communicate with the initial instance.

Convert English to Arabic number without changing any regional settings in .net

Well, most applications that I worked with was multilingual that supports English UI and Arabic UI.

And one of the major issue that we have faced is displaying Arabic numbers without the need of changing the regional settings of the PC.

So the code below will help you to display Arabic number without changing any regional settings.

is it possible to convert whole visual studio 2008 web site project to visual studio 2010 web site p


is it possible to convert whole visual studio 2008 web site project to visual studio 2010 web site project


my current project references




i have a very annoying iis problem

iis server stops working until app pool is recycled


i am hoping that new visual studio (net framework 4.0) may solve this very annoying unsolveable problem


How to cast integer values as an array of enum values?


Hello all!

I'm working with VS2010 Beta2, ASP.NET and Dynamic Data. I'm using VB but will certainly be happy to receive a C# example if necessary. Laughing

I used a few of Steve's examples (Writing Attributes and Extension Methods for Dynamic Data and Dynamic Data - Hiding Columns in selected PageTemplates) to create a custom property attribute to be applied to a dynamic data column and want it to accept an array of enum values (each value will represent a dynamic data page template where this column should not be displayed).  My new attribute works fine when I hardcode it like this, meaning the HideColumnIn attribute is properly populated with the array of enum values:

<HideColumnIn(PageTemplate.List, PageTemplate.Edit)> _
Public Property My_DD_Column As Object


However, I want to store the values to be used for the HideColumnIn in the DB as integer values that correspond to the

merging multiple tables in a single dataset to single table


 i have a stored procedure which returns three tables to a dataset ..... now i need to merge all three tables to a single table from d same dataset 

like dataset1 has table1 table2 and table3 .... i want all the three tabels to be merged into dataset1 itself .... instead of three diffrent tables so that i can show all three table data in a single datagrid  as a compact data and combination of 3 tables from d single dataset.....

can some1 help me please.....

Convert excel file into xml


Hai, I have two columns with id,name in the excel sheet.I want to convert into xml file in the below format

<?xml version="1.0" encoding="utf-8"?>

<guide xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Boat id="1"> <name> Copper Kettle </name> </Boat> <Boat id="2"> <name> Copper Penny </name> </Boat> <Boat id="3"> <name> Copperhead </name> </Boat> <Boat id="4"> <name> Coppertone </name> </Boat> <Boat id="5"> <name> Copy Boy </name> </Boat> </guide>

suggest me with sample code in ASP.NET. Thanks


how To Insert the single quotes in sqlcommand in asp.net


Hi all

    i am developed on simple user registeration application in that i gave  address field .when i am trying to enter something like this (1st main,b'lore) but wont take while inserting.

my query is

sqlcommand cmd=new sqlcommand("insert into registeration_form(name,address)values('"+txtname.text+"','"+txt_address+"')",con);

how can i pass the  quotes also to my query please if any one know reply me.


G Dinakaran

convert SQL data to EDI 835 file


I was assigned to create a app to convert SQL data to EDI 835 file. (Electronic Data Interchange file for Health Care Claim Payment/Advice,  outbound process only).

Can someone help me where to start?

Convert WCF service to self hosting application


 I created project as a  WCF Service application and now according to recuriment that needs to be converted as a self hosting application

How can i do this?

Converting WCF service app to Self hosting app?

How to Convert HTML to Image


 HI all,

I was struggling for the process/code to convert html to image. Please suggest any tutorials for this. And also any code help greatly appreciated.


thanks and regards,



ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend