Welcome to the Parallax Discussion Forums, sign-up to participate.

# Math

I want to type in a number zero to 6000 and multiply it by .682 and make the result
an integer zero to 4095 to send to a 12 bit D/A. I can't get the math to work???
```{{          test program
}}
Con
_Clkmode = Xtal1 + pll16x
_Xinfreq = 5_000_000
multi = 0.682

Var
LONG outval        'end count
long inval        'start count
LONG oneval

Obj
pst:          "Parallax Serial Terminal"

Pub   main
pst.start(115_200)     'check the baud rate
pst.str(string("hello",\$0D))

repeat
pst.rxflush
pst.str(string("Gimmie a number --  ",\$0D))
inval:=pst.Decin
outval := inval * multi
pst.Dec (outval)

```

• edited 2020-11-05 - 16:59:54
Spin only natively uses integers. As in whole numbers.

It would help if you took one step back from how you calculated .682, which can be expressed as a numerator / denominator pair.

Multiply first, then divide.

outval := inval * 4095 / 6000
• Wow! that was E Z... that works fine... I think I missed class that day....

Thanks Whicker...

Bill.

• Or multiply by 1000 you "multi = 0.682" => 682 so you avoid the fractions

And result "outval := (inval * multi) /1000
• edited 2020-11-05 - 17:42:31
If you want to display decimal values you can use the method "DecPoint" as shown below. If you want three digits after the decimal use 1000 for the value of "denominator".

This assumes "value" is scaled by the value of "denominator".
```PUB DecPoint(value, denominator)

if value < 0
Pst.Char("-")
-value

if value => denominator
result := value / denominator
Pst.Dec(result)
value //= denominator
else
Pst.Char("0")
Pst.Char(".")
repeat while denominator > 1
denominator /= 10
if value => denominator
result := value / denominator
Pst.Dec(result)
value //= denominator
else
Pst.Char("0")
```