LoginSignup
1
2

More than 5 years have passed since last update.

Arduino > sprintf() > %fを使えない? > dtostrf()が用意されている

Last updated at Posted at 2016-06-02

avr-libcのマニュアルを読むと使えるような記述がありますが、残念ながら、Arduino Unoのsprintf()では、%fを使うことができないようです。

  char szbuf[200];
  sprintf(szbuf,"Pressure(kPa)=%.2f, Altitude(m)=%.2f\r\n", prs, alt);

を実行するとszbufにはPressure(kPa)=%.2f, Altitude(m)=%.2f\r\nがセットされ、それがudpLoggerに送信され、microSDに保存された。

整数で処理するしかないのだろうか。




には%fを使っているようだ。

未消化。



以下のようにした (間違っているかもしれない)。
void UdpTxAltitude(float prs)
{
  float alt;
  alt = calcAltitude(prs, kAltitudeCorrection);

  char szbuf[200];
  int pos = 0;
  int whl, frac; // whole and fractional parts

  // 1. pressure
  whl = (int)prs;
  frac = (int)(prs*100) % 100;
  pos = sprintf(&szbuf[pos],"Pressure(kPa)=%d.%02d", whl, frac);

  // 2. altitude
  whl = (int)alt;
  frac = (int)(alt*100) % 100;
  pos = sprintf(&szbuf[pos],",Altitude(m)=%d.%02d\r\n", whl, frac);

  WiFi_txMessage(szbuf);  
}



(追記 2016/06/03)
@mt08 さんに dtostrf() という関数の情報とリンクを教えていただきました。
1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2