1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

浮動小数点の89.51を文字列に変換して遊ぶ。

Last updated at Posted at 2024-11-20

参考

x 過去ログを見よ!!
x M5NanoC6での予定は、未定
x 浮動小数点の変換誤差あり

目的
加速度センサーのデータを変換する
軽量、高速化、見やすさ重視

オンラインコンパイラ


#include <iostream>
using namespace std;


void ch_conv(char *buf,float f){
    
    int n; //変換元
    int a; //一時変数
    
    n = (int)(f * 100);

    // 0123456
    // -12.34_

    buf[0]=' ';
    buf[3]='.';
    buf[6]=0;

    //符号を外す
    if(n < 0) {n = 0 - n;buf[0]='-';}

    a = (n * 52429) >> 19; // n / 10;
    buf[5] = '0' + n - (a * 10);
    n = a;

    a = (n * 52429) >> 19; // n / 10;
    buf[4] = '0' + n - (a * 10);
    n = a;

    a = (n * 52429) >> 19; // n / 10;
    buf[2] = '0' + n - (a * 10);
    n = a;

    a = (n * 52429) >> 19; // n / 10;
    buf[1] = '0' + n - (a * 10);
    //n = a;

}//ch_conv


int main(void){
    // Your code here!

    char str1[32];

    ch_conv(str1,89.51);
    
    printf("%s\n",str1);
    
}




 89.51

1
1
3

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?