Recent

Author Topic: Get float results from division  (Read 1678 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Get float results from division
« on: September 16, 2014, 11:39:38 pm »
Since my app works with currency values, im having some troubles to manage this situations

MyPart:= 1.00/3;

Because i get 0.333333... and i need just 2 decimal positions. Im trying to figure a workaround for this, but i do not understand at all formatting.

I need obtain 0.33, always rounding to the lowest.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Get float results from division
« Reply #1 on: September 16, 2014, 11:56:59 pm »
Not sure if there's a function, but this is pretty effective way:
Code: [Select]
MyPart:= trunc(100 * 1.00 / 3) / 100;Should return 0.33.

edit:
But because of float is imperfect in general, it could return 0.330000001 or 0.32999999, which are both equally right. You should format the string too
Code: [Select]
label1.caption := format('%.2f', [1/3]);
« Last Edit: September 17, 2014, 12:03:09 am by User137 »

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: Get float results from division
« Reply #2 on: September 17, 2014, 12:02:44 am »
If you want the result as a string, then the FloatToStrF function is very useful.

 

TinyPortal © 2005-2018