Percentages

Just starting out? Need help? Post your questions and find answers here.
ClueLess
Enthusiast
Enthusiast
Posts: 345
Joined: Sun Jan 11, 2009 1:04 am

Percentages

Post by ClueLess »

Hi

I'm trying to get this code to run, but does not give me the decimals:

Code: Select all

PartCost.f = 88
PartMargin.f = 3,6
  
;   PartCost.f = Val(GetGadgetText(#Txt_Part_Cost))
;   PartMargin.f = Val(GetGadgetText(#Txt_Part_Margin))

  
  PartPVP.f = PartCost + (PartMargin / 100 * PartCost)
  
  MessageRequester("", Str(PartPVP))
Can anyone help me with this?

Thank You
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: Percentages

Post by Bisonte »

not Str() .... use StrF() !
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
ClueLess
Enthusiast
Enthusiast
Posts: 345
Joined: Sun Jan 11, 2009 1:04 am

Re: Percentages

Post by ClueLess »

Thank You. It works.

Can I limit the decimals to two with round up?
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Percentages

Post by Marc56us »

and decimal separator is dot

Code: Select all

PartCost.f = 88
PartMargin.f = 3.6 ; <--------- . not ,
 
;   PartCost.f = Val(GetGadgetText(#Txt_Part_Cost))
;   PartMargin.f = Val(GetGadgetText(#Txt_Part_Margin))

 
  PartPVP.f = PartCost + (PartMargin / 100 * PartCost)
 
  MessageRequester("", StrF(PartPVP, 2)) ; <----- StrF (as Bisonte say)
ClueLess
Enthusiast
Enthusiast
Posts: 345
Joined: Sun Jan 11, 2009 1:04 am

Re: Percentages

Post by ClueLess »

Smothing is wromg

With values of Part_Cost = 55
And part_Margin = 6.5

the result on PB is 58.30
On windows calculator is 58.575



Best regards
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Percentages

Post by RASHAD »

1 :
The decimal symbol can be . or , it depends on
Control Panel --> Region settings

2 :
The result is accurate using PB as Calculator
Just the fraction in your PartMargin.f been ignored maybe due to using wrong Dec.symbol

Code: Select all

PartCost.f = 55
PartMargin.f = 6.5
 
;   PartCost.f = Val(GetGadgetText(#Txt_Part_Cost))
;   PartMargin.f = Val(GetGadgetText(#Txt_Part_Margin))

 
  PartPVP.f = PartCost + (PartMargin / 100 * PartCost)
 
  MessageRequester("", StrF(PartPVP,3))
Egypt my love
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Percentages

Post by skywalk »

This is integer casting its ugly head in a floating point division.
Just make sure to prefix floating point operations with "0.0 + ".
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
ClueLess
Enthusiast
Enthusiast
Posts: 345
Joined: Sun Jan 11, 2009 1:04 am

Re: Percentages

Post by ClueLess »

@RASHAD

Usind . or , as decimal gives the exatly same result
ClueLess
Enthusiast
Enthusiast
Posts: 345
Joined: Sun Jan 11, 2009 1:04 am

Re: Percentages

Post by ClueLess »

@ skywalk

Can you explain? I did not understand what you are trying to say
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: Percentages

Post by Bisonte »

ClueLess wrote:@RASHAD

Usind . or , as decimal gives the exatly same result
In Purebasic-SourceCode this -> "." is the Decimalpoint not this : ","

Code: Select all

Float.f = 12.2345 ; <--- Right
Float2.f = 12,1234 ; <- Wrong
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
ClueLess
Enthusiast
Enthusiast
Posts: 345
Joined: Sun Jan 11, 2009 1:04 am

Re: Percentages

Post by ClueLess »

Found the problem

Code: Select all

Procedure.s CalcPVP()
  PartCost.f = ValF(GetGadgetText(#Txt_Part_Cost)) ; < ----------ValF instead of just val
  PartMargin.f = ValF(GetGadgetText(#Txt_Part_Margin));  < ----------ValF instead of just val

 
  PartPVP.f = PartCost + (PartMargin / 100 * PartCost)
 
;  MessageRequester("", StrF(PartPVP, 2)) ; <----- StrF (as Bisonte say
  
  ProcedureReturn StrF(PartPVP, 3)
__________________________________________________
Code tags repaired
12.03.2018
RSBasic
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Percentages

Post by skywalk »

ClueLess wrote:@ skywalk Can you explain? I did not understand what you are trying to say

Code: Select all

Define.i x1, Xmax, Xr
x1 = 25
Xmax = 250
Debug Str(x1 / Xmax * 100) + "%"        ; = 0% :(
Debug Str(0.0 + x1 / Xmax * 100) + "%"  ; = 10%, Floating point implied by '0.0 +'
Debug StrF(x1 / Xmax * 100, 2) + "%"    ; = 10%, Floating point implied by StrF()?
Debug StrD(x1 / Xmax * 100, 2) + "%"    ; = 10%, Floating point implied by StrD()?
Xr = (x1 / Xmax * 100)                  ; = 0%, Despite assigning to floating point?
Debug StrD(Xr, 2) + "%"                 ; = 0%
Xr = (0.0 + x1 / Xmax * 100)            ; = 10%, Floating point implied by '0.0 +'
Debug StrD(Xr, 2) + "%"                 ; = 10%
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Percentages

Post by Marc56us »

The representation of a decimal number for each country should not be confused with the decimal separator used in programming.
Fortunately, the latter is the same for everyone, otherwise we could not use the same source code depending on the countries. :o
The decimal separator is also the same in basic, C, Pascal etc.

In many langages, you can force transtypage by adding .0 To opération
Try
Debug 3 / 2
Debug 3 / 2.0
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Percentages

Post by Demivec »

skywalk wrote:This is integer casting its ugly head in a floating point division.
Just make sure to prefix floating point operations with "0.0 + ".
@Skywalk: His variables are all floats and they occur first in the equations being assignments to other float variables. This isn't a case of 'integer casting' in this particular instance.
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Percentages

Post by skywalk »

Yes, my mistake. I was going by an earlier error I had with a percentage calculation involving Integer casting. I am on hyper alert to avoid that. :oops:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply