Just starting out? Need help? Post your questions and find answers here.
ClueLess
Enthusiast
Posts: 345 Joined: Sun Jan 11, 2009 1:04 am
Post
by ClueLess » Mon Mar 12, 2018 5:52 pm
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
Bisonte
Addict
Posts: 1233 Joined: Tue Oct 09, 2007 2:15 am
Post
by Bisonte » Mon Mar 12, 2018 5:55 pm
not Str() .... use StrF() !
P ureB asic 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
Posts: 345 Joined: Sun Jan 11, 2009 1:04 am
Post
by ClueLess » Mon Mar 12, 2018 6:10 pm
Thank You. It works.
Can I limit the decimals to two with round up?
Marc56us
Addict
Posts: 1479 Joined: Sat Feb 08, 2014 3:26 pm
Post
by Marc56us » Mon Mar 12, 2018 6:11 pm
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
Posts: 345 Joined: Sun Jan 11, 2009 1:04 am
Post
by ClueLess » Mon Mar 12, 2018 7:10 pm
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
Posts: 4664 Joined: Sun Apr 12, 2009 6:27 am
Post
by RASHAD » Mon Mar 12, 2018 7:25 pm
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
skywalk
Addict
Posts: 4003 Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA
Post
by skywalk » Mon Mar 12, 2018 7:48 pm
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
Posts: 345 Joined: Sun Jan 11, 2009 1:04 am
Post
by ClueLess » Mon Mar 12, 2018 8:37 pm
@RASHAD
Usind . or , as decimal gives the exatly same result
ClueLess
Enthusiast
Posts: 345 Joined: Sun Jan 11, 2009 1:04 am
Post
by ClueLess » Mon Mar 12, 2018 8:38 pm
@ skywalk
Can you explain? I did not understand what you are trying to say
Bisonte
Addict
Posts: 1233 Joined: Tue Oct 09, 2007 2:15 am
Post
by Bisonte » Mon Mar 12, 2018 8:54 pm
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
P ureB asic 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
Posts: 345 Joined: Sun Jan 11, 2009 1:04 am
Post
by ClueLess » Mon Mar 12, 2018 9:04 pm
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
skywalk
Addict
Posts: 4003 Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA
Post
by skywalk » Mon Mar 12, 2018 9:05 pm
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
Posts: 1479 Joined: Sat Feb 08, 2014 3:26 pm
Post
by Marc56us » Mon Mar 12, 2018 9:40 pm
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.
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
Demivec
Addict
Posts: 4091 Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA
Post
by Demivec » Tue Mar 13, 2018 12:00 am
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.
skywalk
Addict
Posts: 4003 Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA
Post
by skywalk » Tue Mar 13, 2018 1:23 am
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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum