如何在字符串中传递浮点变量 [英] How to pass floating point variables in a string

查看:63
本文介绍了如何在字符串中传递浮点变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c语言在LCD上打印字符串。我想在LCD上打印系统的电压和电流。如何在字符串中打印时包含浮点值。我正在使用SPI与LCD通信。



我要打印的是,

28.0 VDC 18.0 A



其中28.0和18.0是浮点输出变量,Vout_Read和Iout_Read。尝试进行SPI传输时,我无法使用printf语句和%f。非常感谢任何建议或帮助



我尝试过:



我试图在没有通用变量的情况下按原样打印消息。我反复拨打不同的特定字符串,每个字符串具有不同的电压和电流额但我想概括一下并提高代码的效率。

I am trying to print strings on LCD using c language. I want to print the voltage and current of my system on the LCD. how do i include float values when printing in a string. I am using SPI to communicate with the LCD.

What i want to print is,
"28.0 VDC 18.0 A"

Where 28.0 and 18.0 are floating point output variables, Vout_Read and Iout_Read. I can't use a printf statement and %f, while trying to do SPI Transfer. Any suggestion or help is greatly appreciated

What I have tried:

I have tried to print messages as is without having a generic variable. I am repeatedly calling different specific strings each with different voltage and current ratings. But i want to generalize this and improve my code's efficiency.

推荐答案

sprintf 是你的朋友。转换你的浮点数等等,以构建一个可以通过SPI输出的格式化字符串。
sprintf is your friend. Convert your floats and whatever to build a formatted string that you can output over SPI.


这篇关于如何在字符串中传递浮点变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆