Displaying integer part of a decimal value

K

kimiraikkonen

Hi,
Sorry if this is so basic but i wanted to know the correct way of
displaying a value's "only" integer part.

I mean; if i declare a value as integer which is actually a decimal
number, it returns upper value after .51 decimal part.

I mean;
Dim number as integer = 12.51 returns 13.

If i;
Dim number as integer = 12.49 returns 12 as well.

I want to return only the integer part (12) in every case without
depending on whatever decimal part is.

Thanks
 
A

Armin Zingler

kimiraikkonen said:
Hi,
Sorry if this is so basic but i wanted to know the correct way of
displaying a value's "only" integer part.

I mean; if i declare a value as integer which is actually a decimal
number, it returns upper value after .51 decimal part.

I mean;
Dim number as integer = 12.51 returns 13.

Option Strict On?


Use
number = cint(int(12.51))


Armin
 
T

Teemu

kimiraikkonen said:
Hi,
Sorry if this is so basic but i wanted to know the correct way of
displaying a value's "only" integer part.

I mean; if i declare a value as integer which is actually a decimal
number, it returns upper value after .51 decimal part.

I mean;
Dim number as integer = 12.51 returns 13.

If i;
Dim number as integer = 12.49 returns 12 as well.

I want to return only the integer part (12) in every case without
depending on whatever decimal part is.

Thanks

Math.Floor for example.

-Teemu
 
H

Herfried K. Wagner [MVP]

kimiraikkonen said:
Sorry if this is so basic but i wanted to know the correct way of
displaying a value's "only" integer part.

'Int', 'Fix', 'Math.Floor', depending on what you want.
 
K

kimiraikkonen

Thanks all. Much ways there were.

As last, i'd like to know how get these functions work with the value
in textbox?

if i type 12.51(dot) in textbox1 and use that code;
Dim number As Integer = TextBox1.Text
MsgBox(Math.Floor(number))

i get: 1251

and if i type 12,51 (comma) in textbox1 and use that code;
Dim number As Integer = TextBox1.Text
MsgBox(Math.Floor(number))

i get: 13 instead of 12

Behaviour is same also with FIX function...

Thanks!
 
A

Armin Zingler

kimiraikkonen said:
As last, i'd like to know how get these functions work with the value
in textbox?

if i type 12.51(dot) in textbox1 and use that code;
Dim number As Integer = TextBox1.Text
MsgBox(Math.Floor(number))

i get: 1251

Enable Option Strict in order to get aware of data types and
conversions.


Armin
 
K

kimiraikkonen

Enable Option Strict in order to get aware of data types and
conversions.

Armin

if i enable option strict i get the error using the code above:
Option Strict On disallows implicit conversions from 'String' to
'Integer'.
 
A

Armin Zingler

kimiraikkonen said:
if i enable option strict i get the error using the code above:
Option Strict On disallows implicit conversions from 'String' to
'Integer'.

Yes, that's the problem: You assign a String to an Integer variable.
Using Math.Floor afterwards doesn't make sense because the value is
already Integer. The conversion takes place internally before. As you
don't do it explicitly, it's not clear what happens. With Option Strict
On, you are forced to do think about it and do the conversion of your
choice.

First you have to convert from String to a number, then cut the
decimal places, then convert to Integer. Conversion from String to
number is culture dependent (usually "." or "," as decimal separator).

Dim number As Integer

number = cint(math.floor(cdec(TextBox1.Text)))
msgbox (number)



Armin
 
K

kimiraikkonen

Yes, that's the problem: You assign a String to an Integer variable.
Using Math.Floor afterwards doesn't make sense because the value is
already Integer. The conversion takes place internally before. As you
don't do it explicitly, it's not clear what happens. With Option Strict
On, you are forced to do think about it and do the conversion of your
choice.

First you have to convert from String to a number, then cut the
decimal places, then convert to Integer. Conversion from String to
number is culture dependent (usually "." or "," as decimal separator).

Dim number As Integer

number = cint(math.floor(cdec(TextBox1.Text)))
msgbox (number)

Armin

Thanks,
Your code works well with decimals seperated by comma, seperating with
dot doesn't work with value in textbox, but thanks however.
 
A

Armin Zingler

kimiraikkonen said:
Thanks,
Your code works well with decimals seperated by comma, seperating
with dot doesn't work with value in textbox, but thanks however.

It does work correctly with the dot. It depends on what you expect. :)
The dot is probably used to group the digits, like "1,000,000.573". It
doesn't have an influence on the value, it's just removed.

If you want to use culture settings different from the system settings
(see control panel), you can use Decimal.Parse and pass an
IFormatProvider, for example a CultureInfo object
(new Globalization.CultureInfo("en-us") for example).


Armin
 
K

kimiraikkonen

It does work correctly with the dot. It depends on what you expect. :)
The dot is probably used to group the digits, like "1,000,000.573". It
doesn't have an influence on the value, it's just removed.

If you want to use culture settings different from the system settings
(see control panel), you can use Decimal.Parse and pass an
IFormatProvider, for example a CultureInfo object
(new Globalization.CultureInfo("en-us") for example).

Armin

Thanks Armin, a shorter way i thought :)

If TextBox1.Text.Contains(".") = True Then
TextBox1.Text = TextBox1.Text.Replace(".", ",")
End If
 
A

Armin Zingler

kimiraikkonen said:
Thanks Armin, a shorter way i thought :)

If TextBox1.Text.Contains(".") = True Then
TextBox1.Text = TextBox1.Text.Replace(".", ",")
End If

You'll get an exception if you enter "1.234,56" and the "," is the
decimal seperator as soon as you try to convert it to a number because
two "," are not allowed.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top