Work with 'n' decimal

  • Thread starter Thread starter BlackSun
  • Start date Start date
B

BlackSun

Hi,
I need to work with number (type long) and I must havo only 6 decimal.
How can I do??

Thank you in advance.

Cheers BlackSun
 
Based on that description, I would simply use a Long and scale it by 10^6.
The other possibility is a decimal. But if you can provide more detail about
exactly what you are trying to do, it might be possible to provide a more
detailed response.
 
Hi,
exactly what you are trying to do, it might be possible to provide a more
detailed response.

Thank you for your reply. You are rigth... I must to be more clear.
I will try to do it with an example:

Dim valore as long = 123,1234567890

And I would like to get 123,12345....

Must I use FormatNumber(valore, 5)?
Is it correct or there are other ways?

Thank you!

Cheers BlackSun
 
If you do

Dim valore as long = 123,1234567890

then the value 123 will be stored in valore and there is no way that you can
recover the digits following the decimal point.

If you do

Dim valore as long = 1231234567890

Then you can create a string version of your number in the format you have
inficated by using:

Dim S As String = FormatNumber(CDbl(valore) / 10.0 ^ 10, 5)

but that will round by default, not truncate (123,12346)

Why do you have to work in type long? Seems to me that you would be better
off using a floating point format.
 
Hi,


Thank you for your reply. You are rigth... I must to be more clear.
I will try to do it with an example:

Dim valore as long = 123,1234567890

And I would like to get 123,12345....

Must I use FormatNumber(valore, 5)?
Is it correct or there are other ways?

Thank you!

Cheers BlackSun

Try this

Dim valore As Decimal
Dim npos As Integer
Dim c As String

' with more than 5 places after the decimal point
valore = 123.123456789
npos = InStr(valore.ToString("##0.0000000000").Trim(), ".")

c = Strings.Left(valore.ToString("##0.0000000000").Trim(),
npos - 1) & "." & _
Strings.Left(valore.ToString("##0.0000000000").Trim
().Substring(npos), 5)
Console.WriteLine("more than 5 decimal places {0}", c)

' with less than 5 places after the decimal point
valore = 123.123
npos = InStr(valore.ToString("##0.0000000000").Trim(), ".")

c = Strings.Left(valore.ToString("##0.0000000000").Trim(),
npos - 1) & "." & _
Strings.Left(valore.ToString("##0.0000000000").Trim
().Substring(npos), 5)
Console.WriteLine("less than 5 decimal places {0}", c)

' exactly 5 places after decimal point
valore = 123.12345
npos = InStr(valore.ToString("##0.0000000000").Trim(), ".")

c = Strings.Left(valore.ToString("##0.0000000000").Trim(),
npos - 1) & "." & _
Strings.Left(valore.ToString("##0.0000000000").Trim
().Substring(npos), 5)
Console.WriteLine("exactly 5 decimal places {0}", c)

' no decimals
valore = 123
npos = InStr(valore.ToString("##0.0000000000").Trim(), ".")

c = Strings.Left(valore.ToString("##0.0000000000").Trim(),
npos - 1) & "." & _
Strings.Left(valore.ToString("##0.0000000000").Trim
().Substring(npos), 5)
Console.WriteLine("no decimals {0}", c)

HTH,

Diego
 
Hi,
Why do you have to work in type long?  Seems to me that you would be better
off using a floating point format.
Because I have to fill a datagridview with values coming from an excel
file, and there are 15 decimal numbers.

Cheers BlackSun
 
That doesn't make sense. Are the numbers integer or floating point? If
they are integer, then you can use Long, but you need to explain how an
integer becomes converted to a number wtih 5 decimal places for display
purposes. This is the scaling by 10 to a power that I mentioned.

But if the numbers are floating point then you should not be using a Long to
store them, as you will lose all the decinal places. And if they are
floating point, and if you use a floating pointer number type (eg, Double)
then your only issue is displaying the number in the right format, for which
the FormatNumber function is probably going to be adequate.

Hence my question - since it seems the numbers are going to end up as
floating point (at least for display purposes) why do you have to work with
type Long?

Hi,
Why do you have to work in type long? Seems to me that you would be better
off using a floating point format.
Because I have to fill a datagridview with values coming from an excel
file, and there are 15 decimal numbers.

Cheers BlackSun
 
Back
Top