Limit your decimal places to whatever...

Code tips and tricks for beginners
Post Reply
Zim
Posts: 280
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 253 times
Been thanked: 128 times

Limit your decimal places to whatever...

Post by Zim »

I keep forgetting this so, I will post it! LOL.
If there is an easier way, please share!

Code: [Local Link Removed for Guests]


battery = 12.8126 
batt$ = str$(battery , "%2.1f")    ' or %2.3f if you want 3 decimal places etc.
battery = val(batt$)
wlog battery                     ' battery now equals 12.8

User avatar
cicciocb
Site Admin
Posts: 1900
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: Limit your decimal places to whatever...

Post by cicciocb »

use of the STR$ is certainly the best way to do.

However, when I was very young, I was used to do like that :

Code: [Local Link Removed for Guests]

battery = 12.8126 
b = int(battery * 10) / 10 '  10 is for one decimal digit, 100 for 2 digits, 1000 for 3 digits, etc.
wlog b
Post Reply