Convert A Double X To A Decimal Y?

Oct 5, 2011

Let's say I want to convert a Double x to a Decimal y. There's a lot of ways to do that:

1. var y = Convert.ToDecimal(x); // Dim y = Convert.ToDecimal(x)
2. var y = new Decimal(x); // Dim y = new Decimal(x)
3. var y = (decimal)x; // Dim y = CType(x, Decimal)
4. -- no C# equivalent -- // Dim y = CDec(x)

Functionally, all of the above do the same thing (as far as I can tell). Other than personal taste and style, is there a particular reason to choose one option over the other?

EDIT: This is the IL generated by compiling the three C# options in a Release configuration:

1. call valuetype [mscorlib]System.Decimal [mscorlib]System.Convert::ToDecimal(float64)
--> which calls System.Decimal::op_Explicit(float64)

[Code]....

View 3 Replies


ADVERTISEMENT

Convert Double To Decimal (latitude / Longitude) In .net?

May 24, 2012

I have a piece of code:

Dim Latitude As Double = row("fltLatitude")
Dim Longitude As Double = row("fltLongitude")

Most of the lat/longs that are being fetched from the db using SP are retrieved in properly, except for a couple of records which do not include a period to indicate a lat/long value.I have tried using these:

Convert.ToDecimal(Latitude)
CDec(Latitude)
decimal.Parse(Latitude)

but none of them are working.

View 1 Replies

Use A Decimal Instead Of A Double (why Then Do Microsoft Use A Double For Most Math Class Functions)

Aug 15, 2011

For greater accuracy I should use a Decimal instead of a Double ( so I've been told ). Why then have Microsoft chosen to use DOUBLE for most of the functions that return a floating point value in the Math Class?

[Code]....

View 9 Replies

.net - Dim X As Decimal = 100.0m Do A Cast From Double To Decimal Implicitly?

Mar 30, 2011

If I have the code:Dim x as Decimal = 100.0m

Is it casting from a double to a decimal implicitly. How would I do this explicitly in vb.net?

View 2 Replies

Convert A Decimal ( Decimal Place Holder = Dot) To A Decimal (decimal Place Holder =comma)?

Dec 20, 2010

how do you convert a decimal ( decimal place holder = dot) to a decimal (decimal place holder =comma)?

View 6 Replies

Decimal And Double Precision

Jun 30, 2009

I am working on an application that allow the user to store data as numbers.These numbers are later on used for calculation. The number can be of any type and are saved as string. The problem is when they are used for calculation how can understand when it is better to treat them as doubles and when as decimals?

View 9 Replies

How A Double And Decimal Treat 3 And 3.0 And 3.00 Different

Jan 6, 2012

How come a double and decimal treat 3 and 3.0 and 3.00 different?

For example:

Dim dbl As Double = 1 / CInt(TextBox1.Text)
MsgBox(dbl)
If I enter 3 in the TextBox I get: 0.33...
If I enter 3.0 I get: 0.033...
If I enter 3.00 I get: 0.0033...

How do I treat them all as 3?

View 3 Replies

Conversion To Integer, Decimal, Double

Jun 9, 2011

So, yes, I've tried with these three formats, and the problem remains the same:I have a string, "s"For Each s As String In stringarray and this array is recieving data from a streamreader that is reading a csv file. The data I'm having dificulty reading are these very specific numbers(yes, only these numbers, because phone numbers get home safe and sound).The numbers I'm talking about are usually decimal.ValueList.Add(Convert.ToDecimal(s))

Now, when I convert them like that, they always bring a "D" alongside with them. I've personally checked the string multiple times, and it just has a "1", and somehow, my list recieves "1D" as a number, same thing happens with decimal numbers(0.29D for example). I've tried with Doubles and Integers, the result is the same.

View 1 Replies

Defects In Type Decimal And Double

Jan 1, 2012

here is a very important matter to be corrected as soon as possible. generally,type double can display an answer correct to more than 20 decimal places. yes,it works fine until here except for exponential system. whenever i try to find a decimal raised to the power of a proper fraction, i get a correct answer but only upto 13 decimal places. that is not fair at all! it is important to get as high precision as possible. consider this example:-

[code...]

i want that number of correct decimal places or approximation,which a windows 7 calculator has. i believe that this is someway easy to get rid of. please support.it is important for programs like calculator,worksheets,databases,etc.

View 12 Replies

Double To Decimal Conversion Error

Feb 11, 2009

I am writing a program that asks the user for their name and the number of pieces they completed. The employees are paid more depending on the amount of pieces they have completed(i.e. <200 = .50 per piece, 200 to 399 = .55 per piece, etc.). I have to display the amount earned when the user enters the info and hits the calculate button. I have to use Select Case.

I was thinking this would be an easy program but I keep getting an error for each Case saying "Option Strict On disallows implicit conversions from 'Double' to 'Decimal'. I don't understand why it thinks it is a double. It says I can correct this problem by adding "Cdec" before each equation(i.e.

[Code]...

View 5 Replies

Why Does VB Change A Decimal To A Double When Using An Exponent

Feb 28, 2011

Dim decMonthlyPayment
As Decimal
Dim decPrincipalAtRetirement

[code].....

View 4 Replies

[2008] Analyzing A Decimal (double) Value?

Feb 26, 2009

I'm struggling to solve this problem and am throwing it out to y'all...

For example, if I had a value of 6.5, (representing 6.5 feet), I'd want to separate that value into two values:

6 - 12" sections
1 - 6" section.

This initial value could be any number from 3-16, in .5 increments. (3, 3.5, 4, 4.5, 5, 5.5, etc.)

View 5 Replies

How To Take 3 Or 4 Decimal Points Of A Double Variable Without Round Off

Apr 18, 2012

I have a double value like 3768.595863. if i round this value then i will get 3768.596 but i want 3768.595.how can i get that?please reply.

View 7 Replies

Round A Double To A Certain Number Of Decimal Places

Jan 8, 2009

is it possible to round a double to a certain number of decimal places

View 1 Replies

Rounding Double Number To 6 Decimal Places

Oct 8, 2011

How to i roundup a double number to 6 decimal places in vb.net?

View 1 Replies

Change A Double To A Precision Of 2 Places To The Right Of Decimal Point?

Nov 22, 2009

how to get 1.83333333333... to 1.83?

Dim temp_1 As String
temp_1 = 5.50 / 3

Label1.Text = String.Format(temp_1, "{0.00}")

View 4 Replies

Point Type (pair Of Values) Using Decimal Or Double?

Apr 24, 2010

Is there such a thing as a type similar to a point, but contains a pair of decimals, or a pair of doubles, rather than integers? I searched the forums and google with no luck. I can't believe this has never come up before, I must have been using the wrong search terms.

View 4 Replies

Split A Double Value Inputted Into A String To Decimal And Fractional?

Feb 28, 2009

Simple requirement to split a double value inputted into a string to decimal and fractional.I enter 12.1 and it splits it into

12 and then 0.0999999999999996

Where is my 0.0000000000000004 gone??? Did the cpu tax it?I have tried a few other tricks to split the number and i get the same result...The fractional results of each attempt

12.0 0 Amazing!!!!
12.1 0.0999999999999996
12.2 0.199999999999999

[code]....

View 6 Replies

Calculator Application - Use Datatype - Variables Be Single Double Decimal ?

Mar 11, 2009

I am writing an calculator application and i dont know what datatype to use. i mean should the variables be single, double, decimal...?

View 1 Replies

Presision Calculations - Using Double Could Only Calculate Values Up To 18 Digits After Decimal

Jul 2, 2009

In the discussion in this thread: [URL] it was reveiled to me that vb.net, using double, could only calculate values up to 18 digits after decimal.... so I started thinking and came up with this class

Public Class LargeNumber
Private pNumber As String

Public Sub New()

[CODE].............

I want to split the string into a series of doubles.... and than combine them into a large number.... The size of each double component should be 18 digits on a 32 bit computer... so that I will have room to add the last carrier...

View 3 Replies

VS 2008 Double To String Without Decimal Point Change To Comma?

Mar 14, 2012

I'm having some trouble converting a double to a string.I have a double value, like 43.64 and I need a string like this: "43.64"If I try to convert the double to a string I always get "43,64" what doesn't work for me..

View 5 Replies

Convert BCD To Decimal?

Dec 7, 2009

I am trying to convert information stored in a file to a decimal (or ASCII) format, then write the contents of that file to a new file.

What I have is a list of files that are generated from a cash register. The documentation that was provided to me says that this particular file I am trying to work with has a length of 6, a width of 6, and that it's BCD type.

I've done quite a few google searches and can't seem to find a good solution. I know BCD numbers should be stored as 0000 format. But the problem I'm getting is when I read each part of the file (using my sr.read) I'm getting full numbers..and I am expecting the 0000 format. I'll get a 0, 0, 0, and then all of the sudden I"ll get a number that's 65,000..then I'll get a 34...and there doesn't seem to be any

I'm sure I'm doing something incorrect here, my knowledge of reading HEX, BCD, etc. is very minimal. I'm guessing it has to do with using a filestream, so I can use the fs.ReadByte() method..but when I tried that my program was literally chugging along for several minutes with no sign of ending.[code]...

View 11 Replies

Convert A Decimal Into A Percentage In Vb?

Feb 1, 2010

How do you convert a decimal into a percentage in visual basic? I have the code to get the problem but I need to move the decimal over two places.

View 8 Replies

Convert A Whole Number Into Decimal?

Jan 14, 2012

How to convert a whole number into decimal..For example 70 to .70 because there are some price in my system that needed to be calculated by grams for example $5 per grams... I am saving the price in my database as an integer..

View 2 Replies

Convert Decimal Into Time?

Aug 18, 2010

[code]...

Very simple, I click a button and it shows the answer in the label. The answer to this is: 00:02:38 (according to excel)

But I want my Vb.net to change the decimals into the time as excel would do so. The answer vb gives me is: 0.001833786. So how can I change a decimal into a time?

View 5 Replies

Convert Decimal To DateTime?

Mar 25, 2011

how to convert Decimal To datetime?

View 4 Replies

Convert Decimal Value To Datetime?

Jun 8, 2009

I have a decimal value and i want to convert it to datetime.

Example:

Dim Excess As Decimal
Dim FixEncodedHours As Decimal
Dim OvertimeLimit As Decimal = 4D

[code]....

How can i convert the decimal result Hr_Ex to datetime?

View 2 Replies

Convert Hex To Decimal Using Program?

Mar 13, 2009

I need to convert hex to a decimal in VB.NET. Found several examples in C#, but when I tried to convert to VB.NET I was not successful. An example of a hexadecimal number that I am trying to convert is"A14152464C203230304232323020572F544947455234352E".

View 7 Replies

Convert String To Decimal?

Oct 7, 2010

How to convert string to decimal ?

input - output
45 - 45.00
45.5 - 45.50
45.51 - 45.51

View 1 Replies

Convert Text To Decimal?

May 11, 2010

The code below works until I try to convert the text box to a decimal. I think it might be because at the time of conversion the text box is empty. If thats the case, where could I do the conversion?[code]...

View 5 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved