Issue With LONG Variable Type

Hello,

I’m working on a coin counter and I’m having a bit of trouble with one of the variable calculations. In short, I need to break down the digits of a variable “banktotal” into separate variables to eventually be displayed on a MAX7219 based 7-segment display. In other words, if “banktotal” is 327.55, I need separate variables for each of the decimal places (variable for the “3”, another for the “2”, ect.). I’ve got it to work except for a value of 655.35. For some reason it sorts the digits out as 6 5 5 3 4 instead of 6 5 5 3 5. Anybody know where I went wrong?

Here’s the code set up for the MAX display and the serial monitor. For now I just manually change the value of “banktotal” and re-upload the code. I’ve also included screenshots of the serial monitor.

* 
* MAX7219 Connected as follows:

pin 12 is connected to the DataIn
pin 11 is connected to the CLK
pin 10 is connected to LOAD or CS

We have only a single MAX72XX.

*/

LedControl lc=LedControl(12,11,10,1);

/* we always wait a bit between updates of the display */

 unsigned long delaytime=500;
 float banktotal = 655.34;        //total dollar value of bank, FOR SOME REASON 655.35 WILL Not display properly 
 long banktotalX100 = 0;          //integer value of bank total, actual value X 100
 int bankhundreths = 0;          //digit in cents place for banktotal
 int banktenths = 0;             //digit in tens of cents place for banktotal
 int bankones = 0;               //digit in dollars place for banktotal
 int banktens = 0;               //digit in tens of dollars place for banktotal
 int bankhundreds = 0;           //digit in hundreds place for banktotal
 int bankthousands = 0;          //digit in thousands place for banktotal

void setup() {

/* The MAX72XX is in power-saving mode on startup, we have to do a wakeup call */
lc.shutdown(0,false);

/* Set the brightness to a medium values */
lc.setIntensity(0,8);

/* and clear the display */
lc.clearDisplay(0);

Serial.begin(9600);
}

void displaytotal(){

  lc.clearDisplay(0);

  delay(delaytime);

  lc.setDigit(1,7,9,false);
  lc.setDigit(1,6,8,false);
  lc.setDigit(0,5,bankthousands,false);
  lc.setDigit(0,4,bankhundreds,false);
  lc.setDigit(0,3,banktens,false);
  lc.setDigit(0,2,bankones,true);
  lc.setDigit(0,1,banktenths,false);
  lc.setDigit(0,0,bankhundreths,false);

  delay(1500);

  
}

void loop() {
  banktotalX100 = (banktotal * 100);
  
  bankhundreths = (banktotalX100%10);
  banktenths = ((banktotalX100/10)%10);
  bankones = ((banktotalX100/100)%10);
  banktens = ((banktotalX100/1000)%10);
  bankhundreds = ((banktotalX100/10000)%10);
  bankthousands = (banktotalX100/100000);
  
  Serial.print(banktotal);
  Serial.print("\t");
  Serial.print(banktotalX100);
  Serial.print("\t");
  Serial.print(bankthousands);
  Serial.print("\t");
  Serial.print(bankhundreds);
  Serial.print("\t");
  Serial.print(banktens);
  Serial.print("\t");
  Serial.print(bankones);
  Serial.print("\t");
  Serial.print(banktenths);
  Serial.print("\t");
  Serial.println(bankhundreths);

  
  displaytotal();

}

Never store monetary values as a float, you will get the type of error you are experiencing. Using a float inevitably results in some numbers being slightly off, because there is not an exact binary representation of the decimal fraction. What happens is 655.35 is being stored as something like 655.3499, then you multiply by 100 and convert to an integer, resulting in 65534 instead of 65535. Store the original amount as an integer, in hundredths of a dollar (cents).

Makes sense. Thanks for the suggestion.