Example #1
0
//------------------------------------------------------------------------------
void ostream::putBool(bool b) {
  if (flags() & boolalpha) {
    if (b) {
      putPgm(PSTR("true"));
    } else {
      putPgm(PSTR("false"));
    }
  } else {
    putChar(b ? '1' : '0');
  }
}
Example #2
0
//------------------------------------------------------------------------------
void ostream::putBool(bool b) {

  if (flags() & boolalpha) {
    if (b) {
      pgm t(PSTR("true"));//////////////////////////////////////////upper/lower////////////
      putPgm(t);
    } else {
      pgm f(PSTR("false"));
      putPgm(f);
    }
  } else {
    putChar(b ? '1' : '0');
  }
}
//------------------------------------------------------------------------------
void ostream::putDouble(double n) {
  uint8_t nd = precision();
  double round = 0.5;
  char sign;
  char buf[13];  // room for sign, 10 digits, '.', and zero byte
  char *end = buf + sizeof(buf) - 1;
  char *str = end;
  // terminate string
  *end = '\0';

  // get sign and make nonnegative
  if (n < 0.0) {
    sign = '-';
    n = -n;
  } else {
    sign = flags() & showpos ? '+' : '\0';
  }
  // check for larger than uint32_t
  if (n > 4.0E9) {
    pgm err(PSTR("BIG FLT"));
    putPgm(err);
    return;
  }
  // round up and separate in and fraction parts
  for (uint8_t i = 0; i < nd; ++i) round *= 0.1;
  n += round;
  uint32_t intPart = n;
  double fractionPart = n - intPart;

  // format intPart and decimal point
  if (nd || (flags() & showpoint)) *--str = '.';
  str = fmtNum(intPart, str, 10);

  // calculate length for fill
  uint8_t len = sign ? 1 : 0;
  len += nd + end - str;

  // extract adjust field
  fmtflags adj = flags() & adjustfield;
  if (adj == internal) {
    if (sign) putch(sign);
    do_fill(len);
  } else {
    // do fill for internal or right
    fill_not_left(len);
    if (sign) *--str = sign;
  }
  putstr(str);
  // output fraction
  while (nd-- > 0) {
    fractionPart *= 10.0;
    int digit = static_cast<int>(fractionPart);
    putch(digit + '0');
    fractionPart -= digit;
  }
  // do fill if not done above
  do_fill(len);
}