HSPポータル
サイトマップ お問い合わせ


HSPTV!掲示板


未解決 解決 停止 削除要請

2019
0518
Agrinputで小数を扱いたいけど窓に0.00000と出るのを避けたい5解決


Agr

リンク

2019/5/18(Sat) 18:06:38|NO.87390

inputで小数の入力を可能にしたいのですが、
a = 0.000
input a
とすると、[0.000000]となってしまい見難いので、[   0]と表示されるようにしたいのですが可能でしょうか?

HSPは数字を代入すると勝手に型が変わるので、
a = 0
input a
で行けるかと思ったのですが小数点以下は切り捨てられて整数になってしまいました



この記事に返信する


沢渡

リンク

2019/5/18(Sat) 19:23:50|NO.87392

入力は文字列形式で行い、入力された文字列を実数に変換するという方法ではどうでしょうか。

a=0.0 text="0" pos 0,0 : objsize 200,30 input text pos 0,30 : objsize 60,30 button gosub "入力",*ent stop *ent a=double(text) dialog a return



Ponyo

リンク

2019/5/18(Sat) 22:33:25|NO.87396

※[少数第n位]を数値にしてください。

strf("%.[少数第n位]f")
strf("%.0f")の場合は整数のみ出力(文字列なので直接的に変数に影響はありません。
strf("%.1f")の場合は2.3や1.4などの少数第一位まで表示します。
strf("%.7f")の場合は2.3122322や1.145148などの少数第七位まで表示します。

参考になれば幸いです(*'▽')



まっきー

リンク

2019/5/19(Sun) 03:53:20|NO.87399

小数点以下で0が続く部分を削除したいということでしょうか?
以下のようなもので大丈夫でしたらどうぞ。

#module #defcfunc kirisute str a b = a if instr(b,0,".") = -1{ return a } else{ split b,".",c,d e = strlen(d) if e = 0{ return a } else{ repeat e if strmid(d,-1,1) = "0"{ d = strmid(d,0,strlen(d) - 1) } else{ break } wait 0 loop if d = ""{ return c } else{ f = c + "." + d return f } } } #global a = "" input a button goto "実行",*carry_out stop *carry_out mes kirisute(a)
初心者なばかりにソースが長くなって申し訳ないです。



沢渡

リンク

2019/5/19(Sun) 13:16:04|NO.87402

初期値を0に限定せず、小数の末尾の0を省略した表記にしたいということなら、
こんな形になるでしょうか。

//関数:realopt(p1,p2) //p1の実数を、電卓のような表記(末尾の0を省略した表記)の文字列にして返す。 //p2は小数部分の最大桁数(省略可能。省略時は8) #module #define global ctype realopt(%1,%2=8) _realopt(%1,%2) #defcfunc _realopt double a,int _b,local c,local d b=limit(_b,0,512) c=strf("%."+str(b)+"f",a) d=0 repeat b if peek(c,strlen(c)-1-cnt)!='0' { d=b-cnt break } loop return strf("%."+str(d)+"f",a) #global a=0.0 //任意の数値に変更して可 text=realopt(a) pos 0,0 : objsize 200,30 input text pos 0,30 : objsize 60,30 button gosub "入力",*ent stop *ent a=double(text) dialog a return



Agr

リンク

2019/5/19(Sun) 14:46:00|NO.87404

色々な方法ありがとうございます
文字列の方法が目から鱗でした!



ONION software Copyright 1997-2023(c) All rights reserved.