# Convert to Decimal in vbScript

1

I have the following code:

``````Porcentaje = 0
valor1  = 255
valor2 = 7336

Porcentaje = valor1 * 100 / valor 2
``````

The result is 3.47600872410033 as I am taking the percentage I want to know if there is a way to convert this result to decimal to obtain 3.47

asked by ARR 14.08.2018 в 19:27
source

1

For starters, it is worth pointing out that you could use a normal rounding to a certain number of decimals with `Round(numero, decimales)` , however for what you indicate in your example, it seems that you are looking for a truncated rather than a rounding. For this you could do the following:

``````Option Explicit

Dim Porcentaje
Dim valor1
Dim valor2
Dim Decimales

Porcentaje = 0
valor1 = 255
valor2 = 7336
Decimales = 2

Porcentaje = int((valor1 * 100 / valor2) * (10^Decimales)) / (10^Decimales)
WScript.Echo "Truncado a dos decimales:", Porcentaje
``````

We multiply the value by 10 raised to the desired quantity and convert it to an integer, with that we achieve truncation, then we divide again by the previous value to obtain a number with the number of decimal places expected.

source
1

So you can round up:

``````Round(Porcentaje, 2)
``````

This is another option:

``````FormatNumber(Porcentaje, 2)
``````