String.toFloat()

설명

타당한 스르링을 float 으로 변환.

The input String should start with a digit. If the String contains non-digit characters, the function will stop performing the conversion. For example, the Strings "123.45", "123", and "123fish" are converted to 123.45, 123.00, and 123.00 respectively. Note that "123.456" is approximated with 123.46. Note too that floats have only 6-7 decimal digits of precision and that longer Strings might be truncated.

문법

myString.toFloat()

매개변수

  • myString: a variable of type String

반환값

  • float 형 실수: 문자열을 실수형 숫자로 변환하여 반환한다. 문자열이 정수를 나타내는 문자열일 경우에도 실수형 타입으로 반환되며, 숫자가 아닐 경우 0.0을 반환한다.

예제 코드

void setup() { Serial.begin(9600); String myString = "20.05"; float myFloat = myString.toFloat(); Serial.println(myFloat); } void loop() { }

시리얼 모니터에 결과:

COM6
Send
20.05
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ 주의 및 경고:

If the string is modified, it is highly recommended using String.reserve() to prevent the memory fragmentation issue

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

※ OUR MESSAGES