I need to calculate the difference between the value of two Labels and I'm doing it correctly with the following code:
Label26.Text = (((CDbl(PREZZO) - CDbl(Label17.Text))
(PREZZO)
is a string.
The problem comes when the difference is very small, like in my case 0.00008
, so I'm getting 8e-5
as result.
How can I get the normal number without the scientific notation?
Edit:
Formatting it with "F5"
seems to work but I still got scientific notation sometimes like the following:
The string PREZZO
corresponds to the value of Price €
Label17.Text
to the value of 1H AGO €
.
Code:
(CDbl(PREZZO) - CDbl(Label16.Text)).tostring("F5")
If you don't have a numeric source for that data and you actually need to parse the content of UI elements, the Culture used when inserting data matters, because not all cultures use a comma as decimal separator: if the current UI Culture (Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture) don't match the input format (you show input that uses a comma instead of a dot - as the InvariantCulture - to separate the decimal part), then the text will not parse correctly or at all.
If you have a text input that uses a specific culture format, you need to parse that input specifying the corresponding CultureInfo.
Numbers don't have a format: if you have a numeric source, use that for the calculations, then present the data using the destination UI Culture to provide a localized representation of those value.
If the input Culture and the current Culture are the same, then you don't need to specify a CultureInfo when parsing string values, since the Culture returned by Thread.CurrentThread.CurrentCulture
is used.
Assuming that the input format is based on the Italian format for decimal numbers (assumption based on the use of the name PREZZO
, which Google Translate detects as Italian), you can create a CultureInfo that provides standard formats used in that culture.
When parsing the string values, pass this CultureInfo to the methods, so the text will be parsed correctly.
Also, since you dealing with currency, don't use Double or CDbl
to parse those values, use Decimal.Parse() instead. For example:
Dim PREZZO = "0,04831"
Dim currentPrice = Label16.Text ' "0,04840"
Dim culture = CultureInfo.CreateSpecificCulture("it-IT")
Dim price = Decimal.Parse(PREZZO, culture)
Dim price1Year = Decimal.Parse(currentPrice, culture)
Dim priceDiff = price1Year - price
Dim priceDiffPercent = priceDiff / price
Now, to present the calculated variation in price and the percentage of the variation, you need to format back those values using the same CultureInfo:
labelDiffPrice.Text = priceDiff.ToString("N5", culture)
labelDiffCurrency.Text = priceDiff.ToString("C5", culture)
labelDiffPercent.Text = priceDiffPercent.ToString("P5", culture)
N5
specifies a number with a precision of 5 decimal values.
C5
species to use Currency format and Symbol defined by the CultureInfo, with a precision of 5 decimal values. This overrides the CultureInfo.NumberFormat.CurrencyDecimalDigits, so it should be used for a specific purpose, as in this case.
P5
a percentage representation of a number multiplied by 100
with a precision of 5 decimal positions.
The two calculated values will be presented as:
' Variation in price
0,00009
' Variation in price expressed in currency
€ 0,00009
' Percentage of the variation
0,18630%
If the input comes directly from a User, use Decimal.TryParse() instead of Decimal.Parse()
to validate the input.
See also: Standard numeric format strings