I have to format number to exact this format "###,###,###.##" and try to write program like this:
Dim myNum as double = 1255.32
Debug.Print(myNum.ToString("###,###,###.##"))
I can do .PadLeft to ensure aligning with fixed-width font but my number is not showed correctly with this format string.
If I write that by using "##0.00" then I haven't thousand separator showed.
In earlier Basic versions that was easy but...
How to get number showed in this format in VB.NET?
If is important my local decimal "point" is "," (comma).
Example:
First source Second source
---------------- ----------------
Price: 97.419,52 97.419,26
Tax: 4.870,98 4.870,96
Brutto: 102.290,50 102.290,24
Temp source
----------------
Price: 0,00
Tax: 0,00 Difference
Brutto: 0,00 - 0,26
CultureInfo gives you much more control on the number format. Sample code:
Dim culture As Globalization.CultureInfo = New Globalization.CultureInfo(Globalization.CultureInfo.CurrentCulture.Name)
culture.NumberFormat.NumberDecimalSeparator = "."
culture.NumberFormat.NumberGroupSeparator = ","
Dim myNum As Double = 1255.32
Debug.Print(myNum.ToString("N", culture))
Related
I have scales application. I want to display i.e. 123.456g
I have uint8 that represents 123 (scales range is 0 - 150g)
I also have uint16 that represents 456 (scales range .0 - .999g)
My code:
val strValue = characteristic.getIntValue(FORMAT_UINT16,0).toFloat() + ((characteristic.getIntValue(FORMAT_UINT16,1) or characteristic.getIntValue(FORMAT_UINT16,2).shl(8)) / 1000f)
Produces 51323.457 instead of 123.456
What is wrong?
You're adding floats, not generating a string.
Convert your digits to String, then concatenate them.
I got a price decimal which sometimes can be either 0.00002001 or 0.00002.
I want to display always 3 zeros from the right if the number is like 0.00002 so I'm looking it to be 0.00002000. If the number is 0.00002001 do not add anything.
I came accross some examples and other examplesin msdn and tried with
price.ToString.Format("{0:F4}", price)
but It doesn't actually change anything in the number.
And in the case number is like 123456789 I want it to display 123.456.789 which I've half solved using ToString("N2") but it's displaying also a .00 decimals which I don't want.
Some special cases here between the fractional and whole numbers, so they need to be handled differently.
Private Function formatWithTrailingZeros(number As Double) As String
If number Mod 1 > 0 Then ' has a fractional component
Return $"{number:0.00000000}"
Else
Dim formattedString = $"{number:N2}"
Return formattedString.Substring(0, formattedString.Length - 3)
End If
End Function
Dim price = 0.00002001
Console.WriteLine(formatWithTrailingZeros(price))
price = 0.00002
Console.WriteLine(formatWithTrailingZeros(price))
price = 123456789
Console.WriteLine(formatWithTrailingZeros(price))
price = 123456789.012345
Console.WriteLine(formatWithTrailingZeros(price))
0.00002001
0.00002000
123,456,789
123456789.01234500
If your second case with 123.456.789 is not based on your current culture, then you may need to replace , with . such as
Return formattedString.Substring(0, formattedString.Length - 3).Replace(",", ".")
Since you are using . both as a decimal separator and a thousands separator, I'm not sure how my example of 123456789.012345000 should look, but since you didn't ask, I'm not going to guess.
Good Day!
I need some idea on how can I format the final value with (2) decimal places/round off of code below.
=Count(Fields!rClosedDate.Value) / Count(Fields!PostDate.Value) * 100 & "%"
Thanks in advance.
You can specify a format for numeric values using the Format property of the TextBox.
Examples of formatting string (full list here):
c: currency
d: decimal
p: percentage
To specify 2 decimal places you can set a formatting string like this: p2
What I hope to achieve:
I want to convert text to DNA (which is a base 4 system, "a,G,T,c")
How I plan to do it:
Convert text string to binary,
Dim BinaryConvert As String = ""
For Each C As Char In Textbox1.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
BinaryConvert &= s
Next
Textbox1.Text = BinaryConvert '//Changes the textbox1.Text into binary form
Then convert binary to base 4 via Pseudocode solution:
if (length of binary String is an odd number) add a zero to the front (leftmost position) of the String.
Create an empty String to add translated digits to.
While the original String of binary is not empty {
Translate the first two digits only of the binary String into a base-4 digit, and add this digit to the end (rightmost) index of the new String.
After this, remove the same two digits from the binary string and repeat if it is not empty.
}
The idea behind converting binary to DNA is simply setting G and T equal to one, with c and a equal to zero (G=T=1, a=c=0).
So all I have to do is convert the string to binary first, and then into base 4, in order to convert text to genetic code. Could you please help me write the code to convert binary to base 4.
Thank you for the help!
Converting to base 4 from base 2 is pretty simple. Since 4 itself is the 2nd power of 2, this means you can simply combine two bits to create one base 4 place (2 bits can represent 4 possible values, while 1 base 4 place can also represent 4 possible values). For example:
11100100 (base 2) = 3210 (base 4)
I am writing some code in Visual Basic.net and have a question.
If I have a long number, that is larger than 1000, how can I format this value to be 1,000 (with a comma) and for this to be stored in a string?
For e.g.
1234 will be stored as 1,234
12345 will be stored as 12,345
123456 will be stored as 123,456
Is this done with a TryParse statement?
May I have some help to so this?
Take a look at The Numeric ("N") Format Specifier
General use:
Dim dblValue As Double = -12445.6789
Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
' Displays -12,445.68
If you are only using integers then the following:
Dim numberString As String = 1234.ToString("N0")
Will show numberString = "1,234" as the "N0" format will not add any figures after a decimal point.
For those wanting to do a currency with commas and decimals use the following: .ToString("$0,00.00")
Using $ notation:
int myvar = 12345;
Console.WriteLine($"Here is my number: {myvar:N0}");