2
2

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-23

参考

x 過去ログを見よ!!
x M5NanoC6での予定は、未定

仕様
99.99 より 大きい場合は、99.99
-99.99 より 小さい場合は、-99.99

丸め

符号を外す
89.51 を 100倍する
8951.??? に 0.5を足す
8951.??? を 整数9851にし、小数点以下を丸める
符号を付ける

オンラインコンパイラ



#include <iostream>
using namespace std;


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

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

    n = (int)(f * 100.0 + 0.5);

    if(n > 9999 ){n = 9999;}

    // 0123456
    // -12.34_

    buf[3]='.';
    buf[6]=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);

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

    
}




 89.51
 39.51


2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?