Hello guys.
I have an integer that is storing the result of a division between two numbers. I usually recieve an result that looks like 5,2496874654 .

How can I limit the variable length so it looks more 'approximated', like 5,25 ?

Thanks in advance!

you need to round off the value to the desired decimal places.

Hi!

Have a look at this code a well:

Dim d As Decimal = 52496874654
        d = CDbl(d.ToString().Substring(0, 3))

Hello guys.
I have an integer that is storing the result of a division between two numbers. I usually recieve an result that looks like 5,2496874654 .

How can I limit the variable length so it looks more 'approximated', like 5,25 ?

Thanks in advance!

If you are using an integer, then you are always going to end up with an integer as a result. Perhaps you need to change the type of variable first?

If you are using an integer, then you are always going to end up with an integer as a result. Perhaps you need to change the type of variable first?

Don't think that this is needed, since the values are numbers. I'm actually divinding bytes until I get KB, MB and GB values.

So, if a file has 168463 Bytes, my program would show 160,6589336395264MB (if I made the math correctly). The point is that I don't need ALL those numbers, 2 are good enough.

Unless that there is another way to retrieve the filesize that will show up already on bytes, KB, MB or GB.

Okay guys, the code posted by ShanHanDev worked, but...

If Tamanho > 1073741824 Then
                    TamanhoReal = Tamanho / 1073741824
                    TamanhoReal = CDbl(TamanhoReal.ToString().Substring(0, 3)) & "GB"
                ElseIf Tamanho > 1048576 And Tamanho < 1073741824 Then
                    TamanhoReal = Tamanho / 1048576
                    TamanhoReal = CDbl(TamanhoReal.ToString().Substring(0, 3)) & "MB"
                ElseIf Tamanho > 1024 And Tamanho < 1048576 Then
                    TamanhoReal = CDbl(TamanhoReal.ToString().Substring(0, 3)) & "KB" '**##HERE I get an error##**
                ElseIf Tamanho < 1024 Then
                    TamanhoReal = Tamanho & " bytes"
                End If

... I get an error. In some files, the division won't have any commas to separate the numbers, so I have an error. I tried to do something with Ifs, but no success. What can I do?

Did you try my suggestion ?

Seems that I edited my last post just after you replied me (:

Yes, I'm using your suggestion. I've changed the kind of my var from integer do decimal and I get the error that I described on my last post.

Why cant you try Math.round function as suggested by debasis?

I've read his post, but didn't knew how to do that. I also forgotten that there is a Math method on .Net Framework. I'll check it as soon as I get home, where my code is resting :D

Dim x as integer=2.34567
X=math.round(x)
X should hold the value after rounding.

Hi!

Please check this code:

Dim Tamanho As Decimal = 576 '1048550 '1048579 '52496874654
        Dim TamanhoReal As String

        Dim index As Integer
        If Tamanho > 1073741824 Then
            TamanhoReal = Tamanho / 1073741824
            index = IIf(Tamanho.ToString().Length > 3, TamanhoReal.ToString().IndexOf(".") + 3, Tamanho.ToString().Length)
            TamanhoReal = CDbl(TamanhoReal.ToString().Substring(0, index)) & "GB"
        ElseIf Tamanho > 1048576 And Tamanho < 1073741824 Then
            TamanhoReal = Tamanho / 1048576
            index = IIf(Tamanho.ToString().Length > 3, TamanhoReal.ToString().IndexOf(".") + 3, Tamanho.ToString().Length)
            TamanhoReal = CDbl(TamanhoReal.ToString().Substring(0, index)) & "MB"
        ElseIf Tamanho > 1024 And Tamanho < 1048576 Then
            TamanhoReal = Tamanho / 1024
            index = IIf(Tamanho.ToString().Length > 3, TamanhoReal.ToString().IndexOf(".") + 3, Tamanho.ToString().Length)
            TamanhoReal = CDbl(TamanhoReal.ToString().Substring(0, index)) & "KB"
        ElseIf Tamanho < 1024 Then
            TamanhoReal = Tamanho & " bytes"
        End If

The Math.Round statement worked perfectly. Sorry if I made you make some code for 'nothing',ShahanDev!

Thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.