Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 01:32 27 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : limiting decimal places to two when reding AIN port

Author Message
desitech
Newbie

Joined: 13/12/2023
Location: Australia
Posts: 2
Posted: 08:21am 21 Dec 2023
Copy link to clipboard 
Print this post

I am reading the input from a Analogue port and want to restrict the reading to two decimal places.
I cant find a math function to do this.
Can anyone assist?
Regards - Graham Hobbs
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4238
Posted: 08:25am 21 Dec 2023
Copy link to clipboard 
Print this post

In displaying the value:

print str$(pin(gpx),2,2)


In limitting the value's resolution

value=int(100*pin(gpx))/100



Volhout
Edited 2023-12-21 18:27 by Volhout
PicomiteVGA PETSCII ROBOTS
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9118
Posted: 08:26am 21 Dec 2023
Copy link to clipboard 
Print this post

Assuming this is for output then use STR$(). Otherwise a2=cint(a*100)/100
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3802
Posted: 10:05am 21 Dec 2023
Copy link to clipboard 
Print this post

  desitech said  I am reading the input from a Analogue port and want to restrict the reading to two decimal places.
I cant find a math function to do this.
Can anyone assist?
Regards - Graham Hobbs

If you store the value in floating point, you can't. There will always (*) be a slight deviation from the exact number you'd like to store.

Typically what is stored is plenty good enough, especially as in your case where the thing you're reading is itself not entirely accurate.

If you really wanted to store it exactly you'd have to jump through some hoops e.g. store 100 times the value as an integer.

(*) nearly always, i.e. except in a few cases where what can be stored is the same as what you want, but these are few

Note: the above is about an inherent issue with floating point on all computers using it. You may have seen the same issue using a calculator and doing such as division by 3 then multiplying by 3 and not getting back the original number.

John
Edited 2023-12-21 20:06 by JohnS
 
desitech
Newbie

Joined: 13/12/2023
Location: Australia
Posts: 2
Posted: 11:29am 21 Dec 2023
Copy link to clipboard 
Print this post

Thanks to everyone for your prompt and great assistance
Regards
Graham Hobbs
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024