I need help in converting an engineering decimal value (floating point) into its hexadecimal representation in VBA... The code below takes the integer part only and not the decimal. I would like to have the floating point represented in hex.
Sub float2hex()
'Declaring variables
hexsimvalue As String, txdata As Double
txdata = 100.25
hexsimvalue = Hex(txdata)
End Sub
Thank you in advance.
Related
Can anyone pls send me a link where the method of converting from Hexadecimal to Decimal is explained? I am not understanding why the "Short" type variable is showing a negative sign in the below example...the literal seems to be the same in both cases. I understand UShort will not take signed values but then where is the negative sign even coming from ...the literals are exactly same in both cases. Is it because I am not understanding the rules of conversion from Hexadecimal to Decimal or is it something else?
Module Module1
Sub Main()
Dim counter As Short = &H8000S 'Output: -32768
Dim flags As UShort = &H8000US 'Output: 32768
Console.WriteLine("O/P: {0} {1}", counter, flags)
Console.ReadLine()
End Sub
End Module
Requesting your help to understand the concept of widening better!
I came across the following statement w.r.t 'Widening Conversion' in VB.Net. From the msdn documentation on the topic I found the following: Widening conversions preserve the source value but can change its representation. This occurs if you convert from an integral type to Decimal, or from Char to String. The link to the page is found below:
https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/widening-and-narrowing-conversions
My Question is as follows: I wrote the following code to understand the meaning of the statement "...preserve the source value but can change its representation". But I see no difference in the output when I print the integer or the decimal. Then what does the phrase mean....what is the meaning of "...can change its representation"?
Module Module1
Sub Main()
Dim i As Integer = 5
Dim d As Decimal = i 'widening
Console.WriteLine(i)
Console.WriteLine(d)
'Both prints "5"...no difference in representation
Console.ReadLine()
End Sub
End Module
Can someone also please give an example to demonstrate how the representation changes when we convert a char value to a string?
It means the internal presentation of the number (in your case). Try to convert, say, Single to Double:
Sub Main(args As String())
Dim sng As Single = 1.23E+32
Dim dbl As Double = sng
Console.WriteLine($"Single: {sng}")
Console.WriteLine($"Double: {dbl}")
End Sub
' Output:
' Single: 1,23E+32
' Double: 1,2300000079302825E+32
How would I go about storing a very large number in number format and not scientific.
Please bear in mind that the number I will be storing is too big for the Long data type.
I've set it as a String.
I have a userform with a command button and a textbox.
Below is the sample code:
Private Sub Cmd_Click()
Dim bits As Integer
Dim out As String
bits = 64
out = 2 ^ (bits - 1)
Txt_Output.Value = out
End Sub
The above will return: 9.22337203685478E+18.
But I want 9223372036854775807.
Can anyone explain how to avoid this?
Thanks in advance.
P.S. I'm hoping to avoid having to use an array.
You can achieve that specific calculation using Decimal data types and a modification to the calculation routine:
Private Sub Cmd_Click()
Dim bits As Integer
Dim out As Variant
Dim i As Long
bits = 64
out = CDec(1)
For i = 1 to bits - 1
out = out * 2
Next
Txt_Output.Value = out
End Sub
By forcing out to be a Variant/Decimal, the calculation does not lose precision as it is being calculated. However some things, such as CDec(2) ^ CDec(63) would still lose precision as the calculation would be done using an intermediate Double precision, so you will need to be very careful as to what calculations you do.
This might give you clues as to how to generalise that method to achieve what you need.
If you have 64-bit Excel, you can use the LongLong data type.
I know that is the simple question but how can i return sum of my variable d with decimal places ?
it's always return me 8, not 8.0. But if my variable will be for examle 4.1 it will return 8.2 how it works with 0 in VB.NET?
Public Shared Sub Main()
Dim d As Decimal = 4.0
Console.WriteLine(d+d)
End Sub
use Decimal.ToString() method while writing to console.
Try something like this, pass the decimal in as a string, or else modify this function, but this should do the trick
Private Function ConvertStringToDec(str As String) As Decimal
Dim temp As String = String.Format(".{0}", str)
Dim d As Decimal
Decimal.TryParse(temp, d)
Return dec
End Function
To expand on David's answer, you can manipulate the output of any string you want by changing the first argument of String.Format. For example, if you want two decimal places you can use:
String.Format("{0:0.00}", d+d)
for three decimal places:
String.Format("{0:0.000}", d+d)
The number before the colon corresponds to the argument number. The numbers after the colon specify the format you want. For example with two arguments using different formats:
String.Format("{0:0.0}, {1:0.00}", d, n)
If you had d=4 and n=2 and you printed this out using the above formatter, you would end up with 4.0 (corresponding to the 0 argument with 0.0 format) and a 2.00 (corresponding to the 1 argument with 0.00 format)
and so on. There are a ton of options, not only for numbers, but for dates and more. Check out this easy-to-read website to understand its use a bit more clearly.
Hope this helps!
This question already has answers here:
How to convert numbers between hexadecimal and decimal
(20 answers)
Closed 9 years ago.
I want to convert a string to a decimal then to a string and then to a decimal again.
I tried:
Dim s As String = "0.7655"
CDec(sDec).ToString("0.00") 'results in: 7653,00
CDec(sDec).ToString 'results in: 7648
CDec(sDec).ToString("N") 'results in: 7.653,00
So none of these work!
Is there no easy function to just convert the exact decimal to its string representation again? Seems like too much work for such a simple task!
Preferably without formatting the string, because in that case I seem to know beforehand how many characters the resulting string should have.
After converting it to string, I also want to convert it back to a decimal again.
In your current culture the decimal separator might be a comma instead of a dot. Using the invariant culture will always use the dot:
Dim s As String = "0.7655"
Dim decValue As Decimal = Decimal.Parse(s, System.Globalization.CultureInfo.InvariantCulture)
Console.WriteLine(decValue.ToString())
You should be able to acheive your answer by using the Parse method.
Dim s As String = "0.7655"
Dim c As Decimal = Nothing
c = Decimal.Parse(s)
Console.WriteLine(c)
You can then use the ToString method to convert back to a string.