Best ways to format float values

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Best ways to format float values

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!

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

adamnt42@gmail.com
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



On 13/07/17 15:09, alexchernoff wrote:

> 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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

Fernando Cabral
In reply to this post by alexchernoff
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



2017-07-13 2:39 GMT-03:00 alexchernoff <[hidden email]>:

> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>



--
Fernando Cabral
Blogue: http://fernandocabral.org
Twitter: http://twitter.com/fjcabral
e-mail: [hidden email]
Facebook: [hidden email]
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.
------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

Charlie
In reply to this post by alexchernoff
alexchernoff wrote
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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

Gianluigi
In reply to this post by alexchernoff
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 <[hidden email]>:

> 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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

Charlie
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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

PICCORO McKAY Lenz
In reply to this post by adamnt42@gmail.com
2017-07-13 1:46 GMT-04:00 bb <[hidden email]>:

> I always round i.e. Print Round(26.6601666666666,-2) -->

the digits see -> .660


> 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



>
>
> b
>
>
>
>
> On 13/07/17 15:09, alexchernoff wrote:
>
>> 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
>> [hidden email]
>> 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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

Gianluigi
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

2017-07-13 17:31 GMT+02:00 PICCORO McKAY Lenz <[hidden email]>:

> 2017-07-13 1:46 GMT-04:00 bb <[hidden email]>:
>
> > I always round i.e. Print Round(26.6601666666666,-2) -->
>
> the digits see -> .660
>
>
> > 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
>
>
>
> >
> >
> > b
> >
> >
> >
> >
> > On 13/07/17 15:09, alexchernoff wrote:
> >
> >> 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
> >> [hidden email]
> >> 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
> > [hidden email]
> > 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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

Tobias Boege-2
On Thu, 13 Jul 2017, Gianluigi wrote:

> 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
>

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

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

Gianluigi
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 :-(

2017-07-13 20:00 GMT+02:00 Tobias Boege <[hidden email]>:

> On Thu, 13 Jul 2017, Gianluigi wrote:
> > 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
> >
>
> 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
>
> ------------------------------------------------------------
> ------------------
> 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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

nando_f
In reply to this post by Gianluigi
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 <[hidden email]>
To: mailing list for gambas users <[hidden email]>
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 <[hidden email]>:
>
> > 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
> > [hidden email]
> > 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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

Gianluigi
Hi Nando,

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

Thank you very much

Gianluigi

2017-07-16 10:07 GMT+02:00 <[hidden email]>:

> 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 <[hidden email]>
> To: mailing list for gambas users <[hidden email]>
> 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 <[hidden email]>:
> >
> > > 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
> > > [hidden email]
> > > 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
> > [hidden email]
> > 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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Best ways to format float values

Gianluigi
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

2017-07-16 11:47 GMT+02:00 Gianluigi <[hidden email]>:

> Hi Nando,
>
> good explanation, suitable for a guys with an elementary school
> preparation like me.
>
> Thank you very much
>
> Gianluigi
>
> 2017-07-16 10:07 GMT+02:00 <[hidden email]>:
>
>> 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 -----------
>> From: Gianluigi <[hidden email]>
>> To: mailing list for gambas users <[hidden email]>
>> 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 <[hidden email]>:
>> >
>> > > 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
>> > > [hidden email]
>> > > 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
>> > [hidden email]
>> > 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
>> [hidden email]
>> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Loading...