[Gambas-user] Rounding to 2 decimals

Jussi Lahtinen jussi.lahtinen at ...626...
Fri Jul 27 20:18:17 CEST 2012


Oh, and before using Val() check whether string you got can be converted to
number.


Dim prmAmnt As Integer
Dim totAmnt As Integer
Dim perAmnt As Float

If IsNumber(prmBox.Text)  = True And If IsNumber(totBox.Text) = True Then

prmAmnt = Val(prmBox.Text)
totAmnt = Val(totBox.Text)
perAmnt = Round(prmAmnt / totAmnt * 100, -2)

Print perAmnt

Else

''Here handle incorrect input of user...

Endif


Jussi



On 27 July 2012 21:11, Jussi Lahtinen <jussi.lahtinen at ...626...> wrote:

> ValueBox doesn't take strings.
> So, I suggest to use TextBoxes and do the conversion properly with Val().
>
> Jussi
>
>
>
>
>
> On 27 July 2012 21:05, rocko <sunblaster5 at ...626...> wrote:
>
>> On Fri, 2012-07-27 at 19:49 +0200, Tobias Boege wrote:
>> > On Fri, 27 Jul 2012, rocko wrote:
>> > > On Fri, 2012-07-27 at 19:25 +0200, Tobias Boege wrote:
>> > > > On Fri, 27 Jul 2012, rocko wrote:
>> > > > > Using '.value instead of .text gives an error:
>> > > > > "Unknown symbol "value" in class TextBox"
>> > > > >
>> > > > > On Fri, 2012-07-27 at 02:05 +0200, LeszekK wrote:
>> > > > > > 'Do not use textboxes for mathematical calculations
>> > > > > >
>> > > > > > Dim prmAmnt As Integer
>> > > > > > Dim totAmnt As Integer
>> > > > > > Dim perAmnt As Single
>> > > > > >
>> > > > > > prmAmnt = prmBox.value 'valuebox! not textbox
>> > > > > > totAmnt = totBox.value 'valuebox! not textbox
>> > > > > > perAmnt = Round((prmAmnt / totAmnt * 100), 2)
>> > > > > > ValueBox1.Value = perAmnt & "%"
>> > > > > >
>> > > > > > I not tested the code but it should work.
>> > > > > >
>> > > >
>> > > > First, I herewith want to join the folks who urge to not top-post.
>> > > >
>> > > > If you have a deeper look at the error message above, you'll notice
>> that you still use TextBox
>> > > > instead of ValueBox, right?
>> > > >
>> > > > Regards,
>> > > > Tobi
>> > >
>> > > Ah yes the 'ol top vs bottom post --oh well
>> > > Yes I figured that out and replaced my textBoxes with valueBoxes
>> > > but still can't get it to work
>> > >
>> > >
>> >
>> > :)
>> > OK, so to sum up, two things were wrong with your very first code:
>> > a) Used TextBox without conversion -> use ValueBox instead
>> > b) To round to decimals, the second argument to Round() must be negative
>> >    -> use -2
>> >
>> > This gives:
>> >
>> > Dim prmAmnt As Integer
>> > Dim totAmnt As Integer
>> > Dim perAmnt As Single
>> >
>> > prmAmnt = prmBox.Value
>> > totAmnt = totBox.Value
>> > perAmnt = Round(prmAmnt / totAmnt * 100, -2)
>> >
>> > Print perAmnt
>> >
>> > This code works for me (but without the ValueBox input stuff, I used
>> > prmAmnt = 2 and totAmnt = 3 hard-coded instead)
>> >
>> > If this does not work, please say what makes you feel that it didn't
>> work.
>> >
>> > Regards,
>> > Tobi
>>
>> No it does not work for me.
>> The last valueBox remains blank, actually it has a zero in it.
>> The valueBox isn't updating it's value to the calculation, I can't
>> seem to figure out why.
>>
>> I added a print statement and it does print the result in the console
>> but it's not rounding to 2 places, using 2 and 3 gives a result of
>> 66.6699982.
>> I double checked the name and it is the same as in the code, ValueBox1,
>> as I did not rename it like I did with the others.
>>
>> I am using: ValueBox1.Value = perAmnt & "%"
>>
>> >
>> >
>> ------------------------------------------------------------------------------
>> > Live Security Virtual Conference
>> > Exclusive live event will cover all the ways today's security and
>> > threat landscape has changed and how IT managers can respond.
>> Discussions
>> > will include endpoint security, mobile security and the latest in
>> malware
>> > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>> > _______________________________________________
>> > Gambas-user mailing list
>> > Gambas-user at lists.sourceforge.net
>> > https://lists.sourceforge.net/lists/listinfo/gambas-user
>>
>>
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Live Security Virtual Conference
>> Exclusive live event will cover all the ways today's security and
>> threat landscape has changed and how IT managers can respond. Discussions
>> will include endpoint security, mobile security and the latest in malware
>> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>> _______________________________________________
>> Gambas-user mailing list
>> Gambas-user at lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/gambas-user
>>
>>
>



More information about the User mailing list