I have to convert number (double) to string like this:
Dim myDouble = 3.14
Dim myDoubleStr = myDouble.ToString ''OR myDouble.ToString("N")
According to my 'culture' settings result is "3,14" what is in most cases OK.
But here are cases that I need string representation of a number with decimal point instead of comma.
In that case I replace char "," with "." like string manipulation.
Is here a way that "ToString" convert a number with decimal point directly when this is needed?
Try
.ToString("F", CultureInfo.InvariantCulture)
More info here
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#DFormatString
You can also have as much precession as you want by specifying the format like this:
Dim myDouble As Double = 3.14159268
Dim myDoubleStr = myDouble.ToString("0.00000") 'The value will be 3.14159
In case you wanted to use Thousands separator, use this format:
Dim myDouble = 961327.1234567890
Dim MyDoubleStr = myDouble.ToString("#,##0.00000")
'The value of MyDoubleStr will be 961,327.12345
Related
The following string was generated by this code:
myString = 12:44:44.6111472
myString = Date.Now.TimeOfDay.ToString
I want to add 16.1234567 seconds and the value of myString and assign the resulting value to a new string called myNewString.
Every function I have tried looses the 7 decimal places.
Is there another method I can use that doesn't require me to break apart the entire string value, modify the desired parts, account for carry's, then re-assemble?
If not, then so-be-it.
But, I would like think there is some method, and I'm just not finding it!
Thanks!
Datetime has some parsing functions that you can use. For example:
Dim mystring As String = "12:44:44.6111472"
Dim dt As System.DateTime = System.DateTime.Parse(mystring)
dt += New TimeSpan(16.1234567 * TimeSpan.TicksPerSecond)
mystring = dt.TimeOfDay.ToString()
'now mystring = "12:45:00.7346039"
Here's another approach similar to Ben J's without the use of timespan (which may make things look simpler):
Dim myString = Date.Now.TimeOfDay.ToString
' parse datetime string and add seconds
Dim myDate = DateTime.Parse(myString).AddSeconds(16.1234567)
' outputs 16.123000 so you still lose a few decimal points but not all 7,
' is this close enough precision for you?
Dim ts = String.Format("{0:N6}", (New TimeSpan(myDate.Ticks) - New TimeSpan(DateTime.Parse(myString).Ticks)).TotalSeconds)
So, I've got a string containing an offset which like this:
Dim myString as String = "&AE6D0"
However, for the BinaryWriter I need it to be an integer like this:
Dim myInt as Integer = &AE6D0
How can I convert myString to myInt?
Remove the "&" sign from the beginning of the string and then convert it like this:
Dim myInt as Integer = Convert.ToInt32("AE6D0", 16)
Source: https://msdn.microsoft.com/en-us/library/vstudio/f1cbtwff%28v=vs.110%29.aspx
(I interpreted your offset as being a hexadecimal value, that's why there is the 16)
I have a variable images that is a String. The value of the images are in this format:
"['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']"
How can I convert the value of the images to something without the double quotes in the beginning and ending (or should I change the String into some other types of variables? The format that I want is this:
['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']
No double quotes
Thanks
The String class has a Replace method that will do that.
Dim clean as String
clean = images.Replace("""", "")
Try this...
Dim s as String = "['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']"
s = s.Replace("'", "").Trim()
Any character or word or phrase or even a sentence is considered a string when it's enclosed in double quotes, but if the value of your string literally has double quotes, like this "SAMPLE", and you want to remove the double quotes, then do something...
Dim s as String = ""['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']""
s = s.Replace("""", "").Trim()
Yes I noticed...double double quotes...I equated s to something that you say is passed from MATLAB, the string literally has double quotes, so to remove this, you replace double double quotes with nothing. That's how you do it in .NET. Your compiler interprets double double quotes as just a single quote :)
This worked for me: (Line had a double quote that I didn't want)
Line = Line.Replace(ChrW(34), "").Trim()
Based on the OP's comments I think there is some confusion. Maybe you are new to .Net, coming from MATLAB.
To re-iterate in VB or C# .Net this is an Empty string:
""
that is a string with length == 0. That is something distinct from a string with the value of a quote or double quote.
To continue:
Dim singleQuoteString As String = "'"
Dim doubleQuoteString As String = """"
and so, what you are asking for
['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']
is not a string.
Now, if you want a Textbox, , console output, or something else to show that value then just take your string, like
Dim listOfImagesAsAString = "['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']"
and assign it to where you want it, like
Console.Write(listOfImagesAsAString)
then the command prompt will show what you are asking for.
Example 1
Dim myStr As String = "38"
I want my result to be 38.000 ...
Example 2
myStr = "6.4"
I want my result to be 6.400
What is the best method to achieve this? I want to format a string variable with atleast three decimal places.
Use FormatNumber:
Dim myStr As String = "38"
MsgBox(FormatNumber(CDbl(myStr), 3))
Dim myStr2 As String = "6.4"
MsgBox(FormatNumber(CDbl(myStr2), 3))
So if you have
Dim thirtyEight = "38"
Dim sixPointFour = "6.4"
Then, the best way to parse those to a numeric type is, Double.Parse or Int32.Parse, you should keep your data typed until you want to display it to the user.
Then, if you want to format a string with 3 decimal places, do somthing like String.Format("{0:N3}", value).
So, if you want a quick hack for the problem,
Dim yourString = String.Format("{0:N3}", Double.Parse("38"))
would do.
Take a look on "Standard Numeric Format Strings"
float value = 6.4f;
Console.WriteLine(value.ToString("N3", CultureInfo.InvariantCulture));
// Displays 6.400
In pseudo code
decpoint = Value.IndexOf(".");
If decpoint < 0
return String.Concat(value,".000")
else
return value.PadRight(3 - (value.length - decpoint),"0")
If it's string keep it as a string. If it's a number pass it as one.
I'm sure this is simple to do but I'm struggling to get this work, I've tried using convert.tostring, decimal.tostring, ctype(object, type) and cstr(object) but without success. I guess I'm trying to change the decial object to a string object and then assign it a value of empty string but always get type mismatch error.
Dim testdecimal as decimal = 0.0
testdecimal = Cstr(testdecimal)
testdecimal = string.empty
Your variable is a Decimal.
It cannot hold a string.
You need to declare a separate variable As String to hold the string.
Although you can't set the value of a decimal variable to String.Empty, you can do something like this...
Dim TestDecimal As Decimal = 0.0
Dim strStringValue As String = IIf(TestDecimal = 0.0, "", TestDecimal.ToString())
MsgBox(strStringValue)
You can't convert a decimal to an empty string. I don't know why you would need to do the above, but I would use an object instead:
Dim test As Object = 10.12345
test = "Hi"
test = String.Empty;
It looks like you just need to create a string representiation of the number, which you can do like this:
'create a decimal variable
Dim testDec As Decimal = 10.12345
'convert decimal to string and then set to empty string
Dim testStr As String = testDec.ToString("N")
testStr = String.Empty
Your problem here is that CSTR converts the value to string and not the object itself so what you are doing is taking a decimal variable then converting its value to string and trying to put the string back into the decimal.
Dim testdecimal as decimal = 0.0 'testDecimal is a decimal type and you are assigning a decimal value
testdecimal = Cstr(testdecimal) 'testDecimal is still a decimal but you are trying to put a string in it Here is your first type mismatch
testdecimal = string.empty ' If this actually had worked it would have made the above line pointless because you just tried to overwrite the value (even though this line did not execute here is your second type mismatch)
What you would need to do is:
Dim NewString as String
Dim testdecimal as decimal = 0.0
NewString = Cstr(testdecimal)
The above takes the decimal value and converts it to string then stores it into a string variable.
Now for the second part of your problem, converting a decimal to an empty string. This is impossible because 0.0 converted to string is still "0.0" string.Empty is "" its just an empty string.
If what you mean is you want to convert a decimal to string BUT if the value is 0.0 then make an empty string you can easily do that with an IF statement.
Basically just do:
Dim NewString as String
Dim testdecimal as decimal = 0.0
if(testdecimal =0.0) Then
NewString = String.Empty
Else
NewString = Cstr(testdecimal)
END IF