Groovy web console

subscribe to the feed Subscribe
to this
site
generateEAN_BE (via #groovywebconsole)
tweet this snippet Tweet
this
script

generateEAN_BE

Published 3 months ago by Vaibhav with tags ean
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
	String randomNumberForEAN = "54144" + String.valueOf((long)(Math.random()*(9999999999L-1000000000L)+1000000000L)) + "40";
	char[] charArray = randomNumberForEAN.toCharArray();

	int sum = (((Character.getNumericValue(charArray[0]))+
   (Character.getNumericValue(charArray[2]))+
   (Character.getNumericValue(charArray[4]))+
   (Character.getNumericValue(charArray[6]))+
   (Character.getNumericValue(charArray[8]))+
   (Character.getNumericValue(charArray[10]))+
   (Character.getNumericValue(charArray[12]))+
   (Character.getNumericValue(charArray[14]))+
   (Character.getNumericValue(charArray[16])))*3) +
   (((Character.getNumericValue(charArray[1]))+
   (Character.getNumericValue(charArray[3]))+
   (Character.getNumericValue(charArray[5]))+
   (Character.getNumericValue(charArray[7]))+
   (Character.getNumericValue(charArray[9]))+
   (Character.getNumericValue(charArray[11]))+
   (Character.getNumericValue(charArray[13]))+
   (Character.getNumericValue(charArray[15]))));

	String finalEAN = randomNumberForEAN + String.valueOf(Math.round(Math.ceil(sum/10))*10 - sum);
	System.out.println("finalEAN:"+finalEAN);