Odd / Even numbers

The 1C:Enterprise developers forum

#1
People who like this:0Yes/0No
Interested
Rating: 11
Joined: Nov 10, 2011
Company: 1A Software e.U

Hello,

I could not find how can I do something if I have even (2, 4, 6 ...) or odd (1, 2, 3...) numbers.

That's why I wrote a strange code:

Code

Count = 1;
While SelectionAddresses.Next() Do
 Check = Count / 2;
 OddEven = "" + Check + ""; // here I did not find toString();
 Message (OddEven);
 OddEven = StrOccurenceCount(OddEven, ","); // here is comma because I have 
                                            //German numbers
 If OddEven = 1 Then
   Message ("Odd");
 Else
   Message ("Even");
 EndIf;
 Count = Count + 1;
EndDo()


It works but it is a really strange way. Is there something better?

Thanks!
Regards,

Lioudmila

 
#2
People who like this:0Yes/1No
Active user
Rating: 7
Joined: Nov 3, 2011
Company: 1C Company

Lioudmila,

you can use the "remainder of division" arithmetical operation. It has the following syntax:

Code
Count % 2

This returns 0 for even numbers and 1 for odd numbers. And you no longer need the conversion to string. If you need that conversion elsewhere, please use the Format function of the global context.

 
#3
People who like this:0Yes/0No
Just came
Rating: 0
Joined: Jun 13, 2017
Company:

There is function String(var) for conversion to string

 
#4
People who like this:0Yes/0No
Interested
Rating: 11
Joined: Nov 10, 2011
Company: 1A Software e.U

Thanks a lot, Yulia and Dmitry!

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)
Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.