문제
다음 Chuck 코드가 있습니다.
"examples/vento.txt" => string filename;
FileIO fio;
// open a file
fio.open(filename, FileIO.READ);
// ensure it's ok
if(!fio.good()) {
cherr <= "can't open file: " <= filename <= " for reading..." <= IO.newline();
me.exit();
}
fio.readLine() => string velocity;
fio.readLine() => string direction;
텍스트 파일에는 다음이 포함됩니다.
10
12
(매분마다 Python으로 업데이트됩니다)
속도와 방향을 int(또는 더 나은 float)로 변환하고 싶습니다.
어떻게 해야 하나요?
해결책
사용 atoi
그리고 atof
에서 Std
도서관.0-127(MIDI 속도)을 0과 1.0 사이의 부동 소수점(단위 생성기에 훨씬 더 편리함)으로 변환한다고 가정해 보겠습니다.
Std.atoi(fio.readLine()) => int midi_velocity;
midi_velocity/127.0 => float velocity;
<<< velocity >>>;
인쇄해야 함 0.078740 :(float)
10을 입력하려면
또는 그냥 플로팅으로 바로 이동하려면 다음을 수행하세요.
Std.atof(fio.readLine()) => float velocity;
<<< velocity >>>;
인쇄하는 것 10.000000 :(float)
.
제휴하지 않습니다 StackOverflow