Problemas con truncamiento de número - Forum

Forum Navigation
You need to log in to create posts and topics.

Problemas con truncamiento de número

Hola gente:

Estoy en un problema intentando truncar un número.

Si hago:

Math "32756/256" 2 [Result]

El resultado me da: 127.95

Lo que quiero es truncarlo ya que si no pongo decimales el resultado cambia a 128 como si lo redondeara hacia arriba, muy raro ya que no debería hacerlo.

Parece tarea fácil pero el truncar con Javascript no me ha funcionado en algunos navegadores (IE por ejemplo), podría convertir el número a cadena y quitarle los caracteres finales pero antes quería saber si puede existir una opción más lógica....no he encontrado dentro de Math la opción Truncate, la cual creí que estaba metida cuando indicabas a VisualNeo que no utilizara decimales.

El 32756 será variable y lo que quiero es que quede el numero que esta delante de la coma, sin redondear sin nada, en este caso 127

@palamar

This would be the Javascript way ...

SetVar "[myNumber]" 32756

BeginJS
$App.Result = parseInt($App.myNumber/256);
EndJS

AlertBox "Result" "[Result]" ""

I tested it on Chrome ... if IE has an issue doing this correctly, try this ...

SetVar "[myNumber]" 32756

Math "[myNumber]/256" 12 [Result]
Math "[myNumber]/256" 0 [ResultRounded]
AlertBox "Before" "[myNumber] ... [Result] ... [ResultRounded]" ""

If "[ResultRounded]" ">" "[Result]"
   Math "[ResultRounded]-1" 0 "[Result]"
EndIf

AlertBox "Result" "[Result]" ""

... this may still not give correct answer in (rare) cases e.g. the real answer (13 digits) is something like 123.9999999999996 ... which would make [Result] 124.

Here is a bullet proof method ...

SetVar "[myNumber]" 32756

Math "[myNumber]/256" 0 [Result]
Math "[Result]*256" 0 [ResultInteger]
AlertBox "Before" "[myNumber] ... [Result] ... [ResultInteger]" ""
If "[ResultInteger]" ">" "[myNumber]"
   Math "[Result]-1" 0 "[Result]"
EndIf


AlertBox "Result" "[Result]" ""

 

@palamar @gaev I think this should work too:

Math "32756/256" 2 [Result]
slParseInt "[Result]" [Result]

Regards.

Try this.

Math "32756/256" 2 [Result]
MathAbs [Result] [Abs_Result]

@cn_iceman

Nice find ... command is not documented in the Help file.

Unfortunately, it did not work as expected ... this code ...

Math "32756/256" 2 [Result]
MathAbs [Result] [Abs_Result]
AlertBox "Abs_Result" "[Abs_Result]" ""

... displays 127.95 ... NOT 127

Note: the command wizard says 'Angle in degrees' for the first parameter ... could it be some kind of trigonometric function ?

Note: the command wizard says 'Angle in degrees' for the first parameter ... could it be some kind of trigonometric function ?

Sorry, that's an error in the text. MathAbs returns the absolute value of a number. That means the number without a sign, so any negative number will become positive and any positive number will remain as it is. So I don't think @cn_iceman solution will work.  will fix the text error in the next release. Thank you!

CN_Iceman has reacted to this post.
CN_Iceman

Gracias por el comando, la verdad no lo conocía!.