Discussion:
Best ways to format float values
(too old to reply)
alexchernoff
2017-07-13 05:39:53 UTC
Permalink
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.
bb
2017-07-13 05:46:08 UTC
Permalink
I always round i.e. Print Round(26.6601666666666,-2) --> which is 26.66
by the way, not 26.67! (I have no idea how you got that.)

b
Post by alexchernoff
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
PICCORO McKAY Lenz
2017-07-13 15:31:33 UTC
Permalink
Post by bb
I always round i.e. Print Round(26.6601666666666,-2) -->
the digits see -> .660
Post by bb
which is 26.66 by the way, not 26.67! (I have no idea how you got that.)
due 660 and -2 said the firts two digits, the round will take effect only
in last digit.. due the last no have another mayor than 5, then let in "6"

sorry i dont know how to explain, i deal with this several months ago and
solve it in that way
Post by bb
b
Post by alexchernoff
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.c
om/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
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
Gianluigi
2017-07-13 15:42:00 UTC
Permalink
I would not be misunderstood.
I had understood that Alex wanted a forced increase "Round".

To recap:

Dim n As Float = 26.6601666666666
Dim b As Byte

Print Int(n * 100) / 100 ' Normal truncate, as already
mentioned by other
Print Round(n, -2) ' Normal round, as already
mentioned by other
If Len(CStr(Frac(n))) > 4 Then
b = Val(Mid(CStr(Frac(n)), 5, 1))
If b >= 5 Then
Print Round(n, -2)
Else
Print Round(n + 0.01, -2) ' Forced increase round, as
proposed by me
Endif
Endif

Regards

Gianluigi
Post by PICCORO McKAY Lenz
Post by bb
I always round i.e. Print Round(26.6601666666666,-2) -->
the digits see -> .660
Post by bb
which is 26.66 by the way, not 26.67! (I have no idea how you got that.)
due 660 and -2 said the firts two digits, the round will take effect only
in last digit.. due the last no have another mayor than 5, then let in "6"
sorry i dont know how to explain, i deal with this several months ago and
solve it in that way
Post by bb
b
Post by alexchernoff
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.c
om/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
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
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
Tobias Boege
2017-07-13 18:00:19 UTC
Permalink
Post by Gianluigi
I would not be misunderstood.
I had understood that Alex wanted a forced increase "Round".
Dim n As Float = 26.6601666666666
Dim b As Byte
Print Int(n * 100) / 100 ' Normal truncate, as already
mentioned by other
Print Round(n, -2) ' Normal round, as already
mentioned by other
If Len(CStr(Frac(n))) > 4 Then
b = Val(Mid(CStr(Frac(n)), 5, 1))
If b >= 5 Then
Print Round(n, -2)
Else
Print Round(n + 0.01, -2) ' Forced increase round, as
proposed by me
Endif
Endif
I would avoid arithmetic operations involving even more floats, such as you
proposed. Consider this:

$ gbx3 -e 'Round(0.80999999950+0.1,-9)'
0.909999999
$ gbx3 -e 'Round(0.80999999951+0.1,-9)'
0.91

I only changed the very last digit (of order 10^-11) from 0 to 1 which
shouldn't influence the rouding to 9 decimals at all -- but it does!

The problem here is, famously, that the decimal 0.1 has no finite binary
representation, so *storing* the value that is represented in decimal by
the string "0.1" in a binary float already gives you an unavoidable error.
Whenever you use 0.1 in your program, this error propagates. Specifically,
the error when storing 0.1 in a Single is about 1.49*10^-9, which is how
I arrived at that example above.

Of course, the same applies to the 0.01 you use above. You can think
by yourself about a similar example where float addition with 0.01 makes
the result of a later rounding unreliable.

The other method

Floor(n*100)/100 ' round down to two decimals
Ceil(n*100)/100 ' round up to two decimals

is reliable, since the *integer* 100 can be stored without error in a
float and IEEE754 requires the outcome of float arithmetic to be the
same as if the operation was performed exactly and then rounded to the
limited precision of the float datatype [1].

Regards,
Tobi

[1] http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#865
--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
Gianluigi
2017-07-13 21:14:17 UTC
Permalink
Great Tobias,

You are a good teacher and I am honored to have your attention.
OK I understand, never more extravagant mathematics.
Thank you very much for your explanations

Regards
Gianluigi

P.S. You will not believe it but some tests with Floor and Ceil I did :-(
Post by Tobias Boege
Post by Gianluigi
I would not be misunderstood.
I had understood that Alex wanted a forced increase "Round".
Dim n As Float = 26.6601666666666
Dim b As Byte
Print Int(n * 100) / 100 ' Normal truncate, as already
mentioned by other
Print Round(n, -2) ' Normal round, as already
mentioned by other
If Len(CStr(Frac(n))) > 4 Then
b = Val(Mid(CStr(Frac(n)), 5, 1))
If b >= 5 Then
Print Round(n, -2)
Else
Print Round(n + 0.01, -2) ' Forced increase round, as
proposed by me
Endif
Endif
I would avoid arithmetic operations involving even more floats, such as you
$ gbx3 -e 'Round(0.80999999950+0.1,-9)'
0.909999999
$ gbx3 -e 'Round(0.80999999951+0.1,-9)'
0.91
I only changed the very last digit (of order 10^-11) from 0 to 1 which
shouldn't influence the rouding to 9 decimals at all -- but it does!
The problem here is, famously, that the decimal 0.1 has no finite binary
representation, so *storing* the value that is represented in decimal by
the string "0.1" in a binary float already gives you an unavoidable error.
Whenever you use 0.1 in your program, this error propagates. Specifically,
the error when storing 0.1 in a Single is about 1.49*10^-9, which is how
I arrived at that example above.
Of course, the same applies to the 0.01 you use above. You can think
by yourself about a similar example where float addition with 0.01 makes
the result of a later rounding unreliable.
The other method
Floor(n*100)/100 ' round down to two decimals
Ceil(n*100)/100 ' round up to two decimals
is reliable, since the *integer* 100 can be stored without error in a
float and IEEE754 requires the outcome of float arithmetic to be the
same as if the operation was performed exactly and then rounded to the
limited precision of the float datatype [1].
Regards,
Tobi
[1] http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#865
--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
------------------------------------------------------------
------------------
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
Fernando Cabral
2017-07-13 06:01:31 UTC
Permalink
I can say this is the best way to do what you want, but I use the following:





* Print Int(100.559999 * 100) / 100.00 Print Round(100.559999, -2)*
The first operation truncates the number to 100.55 (no rounding takes
effect).
The second operation rounds the number to at most two digits after the
decimal separator.
This means in the example above, result will be 100.56. If the number was
100.59999
it would be rounded to 100.6.

Again, I dont know if those are the best way to do it, but it works.

Regards

-fernando
Post by alexchernoff
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
--
Fernando Cabral
Blogue: http://fernandocabral.org
Twitter: http://twitter.com/fjcabral
e-mail: ***@gmail.com
Facebook: ***@fcabral.com.br
Telegram: +55 (37) 99988-8868
Wickr ID: fernandocabral
WhatsApp: +55 (37) 99988-8868
Skype: fernandojosecabral
Telefone fixo: +55 (37) 3521-2183
Telefone celular: +55 (37) 99988-8868

Enquanto houver no mundo uma só pessoa sem casa ou sem alimentos,
nenhum político ou cientista poderá se gabar de nada.
Charlie
2017-07-13 09:13:20 UTC
Permalink
Post by alexchernoff
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!
You said 'Format' so you could use: -

Print Format("26.6601666666666", "#.00") '26.66
Print Format("26", "#.00") '26.00
Print Format("1.533", "#.00") '1.53




-----
Check out www.gambas.one
--
View this message in context: http://gambas.8142.n7.nabble.com/Best-ways-to-format-float-values-tp59733p59740.html
Sent from the gambas-user mailing list archive at Nabble.com.
Gianluigi
2017-07-13 10:30:23 UTC
Permalink
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
Post by alexchernoff
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
Charlie
2017-07-13 15:12:39 UTC
Permalink
Also consider: -

Public Sub Main()
Dim fNum As Float[] = [26.6601666666666, 26.6651666666666] 'Note 26.660..
and 26.665..
Dim fTemp As Float

For Each fTemp In fNum
fTemp *= 100
fTemp += 0.5
fTemp = Int(fTemp)
fTemp /= 100
Print fTemp;;
Next

'Output 26.66 26.67

End




-----
Check out www.gambas.one
--
View this message in context: http://gambas.8142.n7.nabble.com/Best-ways-to-format-float-values-tp59733p59743.html
Sent from the gambas-user mailing list archive at Nabble.com.
n***@nothingsimple.com
2017-07-16 08:07:01 UTC
Permalink
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 <***@gmail.com>
To: mailing list for gambas users <gambas-***@lists.sourceforge.net>
Sent: Thu, 13 Jul 2017 12:30:23 +0200
Subject: Re: [Gambas-user] Best ways to format float values
Post by Gianluigi
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
Post by alexchernoff
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
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------
Gianluigi
2017-07-16 09:47:45 UTC
Permalink
Hi Nando,

good explanation, suitable for a guys with an elementary school preparation
like me.

Thank you very much

Gianluigi
Post by n***@nothingsimple.com
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 -----------
Sent: Thu, 13 Jul 2017 12:30:23 +0200
Subject: Re: [Gambas-user] Best ways to format float values
Post by Gianluigi
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
Post by alexchernoff
Peace to all,
this might a bit silly but what is the best way to format float values
like
Post by Gianluigi
Post by alexchernoff
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
------------------------------------------------------------
------------------
Post by Gianluigi
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------
------------------------------------------------------------
------------------
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
Gianluigi
2017-07-16 12:53:20 UTC
Permalink
The street indicated by Tobias should be the safest, right?

I try to attach Adrien's link instead of the code:
https://gambas-playground.proko.eu/?gist=f5dbde82ee8f5f1f5799184108fd57c1

Regards
Gianluigi
Post by Gianluigi
Hi Nando,
good explanation, suitable for a guys with an elementary school
preparation like me.
Thank you very much
Gianluigi
Post by n***@nothingsimple.com
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 <(516)%20666-6666> ' 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 -----------
Sent: Thu, 13 Jul 2017 12:30:23 +0200
Subject: Re: [Gambas-user] Best ways to format float values
Post by Gianluigi
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
Post by alexchernoff
Peace to all,
this might a bit silly but what is the best way to format float
values like
Post by Gianluigi
Post by alexchernoff
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
------------------------------------------------------------
------------------
Post by Gianluigi
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------
------------------------------------------------------------
------------------
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
Continue reading on narkive:
Loading...