[Gambas-user] Best ways to format float values

nando_f at ...951... nando_f at ...951...
Sun Jul 16 10:07:01 CEST 2017


Rounding depends on what rounding you want.
Most people think of 5/4 rounding.
I prefer to round manually 5/4... 5 up, 4 down.

Dim n as Float = 26.660166666666

n = n * 100.  'n = 2666.0166666666   ' 2 decimal points rounding
n = n + 0.5   'n = 2666.5166666666   ' 5/4 rounding*
n = CLong(n)  'n = 2666
n = n / 100.  'n = 26.66


*If you wish to round down, do not add 0.5.  (add 0.0)
*If you wish to round up, add 0.999999999

If you wish to do 3 digits, then *1000 and /1000.
This works for any type of rounding scheme and any reasonable number of digits.

-Nando(Canada)


--
Open WebMail Project (http://openwebmail.org)


---------- Original Message -----------
From: Gianluigi <bagonergi at ...626...>
To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
Sent: Thu, 13 Jul 2017 12:30:23 +0200
Subject: Re: [Gambas-user] Best ways to format float values

> One thing that could be useful?
> 
> Public Sub Main()
> 
>   Dim n As Float = 26.6601666666666
>   Dim b As Byte
> 
>   b = Val(Mid(CStr(Frac(n)), 5, 1))
>   Print Round(n, -2)
>   If b >= 5 Then
>     Print Round(n, -2)
>   Else
>     Print Round(n + 0.01, -2)
>   Endif
> 
> End
> 
> Regards
> Gianluigi
> 
> 2017-07-13 7:39 GMT+02:00 alexchernoff <alexchernoff at ...67...>:
> 
> > Peace to all,
> >
> > this might a bit silly but what is the best way to format float values like
> > 26.6601666666666 into having two digits AND/OR  with rounding them?
> >
> > e.g.
> >
> > 26.6601666666666 becomes 26.66 shortened
> > 26.6601666666666 becomes 26.67 rounded
> >
> > cheers!
> >
> >
> >
> >
> >
> > --
> > View this message in context: http://gambas.8142.n7.nabble.
> > com/Best-ways-to-format-float-values-tp59733.html
> > Sent from the gambas-user mailing list archive at Nabble.com.
> >
> > ------------------------------------------------------------
> > ------------------
> > Check out the vibrant tech community on one of the world's most
> > engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------





More information about the User mailing list