四捨五入

四捨五入にはroundを使います。

他の言語のように、カンマで有効桁数を指定する方式ではないので注意です。

val number:Double = 1.23456
println(Math.round(number))                     // 小数第1位四捨五入 : 1
println(Math.round(number * 10.0) / 10.0)       // 小数第2位四捨五入 : 1.2
println(Math.round(number * 100.0) / 100.0)     // 小数第3位四捨五入 : 1.23
println(Math.round(number * 1000.0) / 1000.0)   // 小数第4位四捨五入 : 1.235

 

 

切り上げ

切り上げにはceilを使います。

val number:Double = 1.23456
println(Math.ceil(number))                     // 小数第1位四捨五入 : 2.0
println(Math.ceil(number * 10.0) / 10.0)       // 小数第2位四捨五入 : 1.3
println(Math.ceil(number * 100.0) / 100.0)     // 小数第3位四捨五入 : 1.24
println(Math.ceil(number * 1000.0) / 1000.0)   // 小数第4位四捨五入 : 1.235

 

 

切り捨て

切り上げにはfloorを使います。

val number:Double = 1.23456
println(Math.floor(number))                   // 小数第1位四捨五入 : 1.0
println(Math.floor(number * 10.0) / 10.0)     // 小数第2位四捨五入 : 1.2
println(Math.floor(number * 100.0) / 100.0)   // 小数第3位四捨五入 : 1.23
println(Math.floor(number * 1000.0) / 1000.0) // 小数第4位四捨五入 : 1.234

 

説明

全角から半角へ置換する

 

引数

引数1(target:String):置換対象の文字列

 

返り値

置換後の文字列

 

コード

/*
=========================================================
[概  要] 全角から半角へ置換する
[引  数] target        : 変換対象の文字列
[戻り値] 変換後の文字列
[備  考]
=========================================================
*/
fun convertIntoHalfFromFull(target : String ):String
{
    var str : String

    //アルファベット(大文字)の変換
    str = target.replace("A","A" ).replace("B","B" ).replace("C","C" ).replace("D","D" ).replace("E","E" ).replace("F","F" ).replace("G","G" ).replace("H","H" ).replace("I","I" ).replace("J","J" ).replace("K","K" ).replace("L","L" ).replace("M","M" ).replace("N","N" ).replace("O","O" ).replace("P","P" ).replace("Q","Q" ).replace("R","R" ).replace("S","S" ).replace("T","T" ).replace("U","U" ).replace("V","V" ).replace("W","W" ).replace("X","X" ).replace("Y","Y" ).replace("Z","Z" ) 
    //アルファベット(小文字)の変換
    str = str.replace("a","a" ).replace("b","b" ).replace("c","c" ).replace("d","d" ).replace("e","e" ).replace("f","f" ).replace("g","g" ).replace("h","h" ).replace("i","i" ).replace("j","j" ).replace("k","k" ).replace("l","l" ).replace("m","m" ).replace("n","n" ).replace("o","o" ).replace("p","p" ).replace("q","q" ).replace("r","r" ).replace("s","s" ).replace("t","t" ).replace("u","u" ).replace("v","v" ).replace("w","w" ).replace("x","x" ).replace("y","y" ).replace("z","z" )
    //数値の変換
    str = str.replace("0","0" ).replace("1","1" ).replace("2","2" ).replace("3","3" ).replace("4","4" ).replace("5","5" ).replace("6","6" ).replace("7","7" ).replace("8","8" ).replace("9","9" )
    //カタカナの変換
    str = str.replace("ア","ア" ).replace("イ","イ" ).replace("ウ","ウ" ).replace("エ","エ" ).replace("オ","オ" ).replace("カ","カ" ).replace("キ","キ" ).replace("ク","ク" ).replace("ケ","ケ" ).replace("コ","コ" ).replace("サ","サ" ).replace("シ","シ" ).replace("ス","ス" ).replace("セ","セ" ).replace("ソ","ソ" ).replace("タ","タ" ).replace("チ","チ" ).replace("ツ","ツ" ).replace("テ","テ" ).replace("ト","ト" ).replace("ナ","ナ" ).replace("ニ","ニ" ).replace("ヌ","ヌ" ).replace("ネ","ネ" ).replace("ノ","ノ" ).replace("ハ","ハ" ).replace("ヒ","ヒ" ).replace("フ","フ" ).replace("ヘ","ヘ" ).replace("ホ","ホ" ).replace("マ","マ" ).replace("ミ","ミ" ).replace("ム","ム" ).replace("メ","メ" ).replace("モ","モ" ).replace("ヤ","ヤ" ).replace("ユ","ユ" ).replace("ヨ","ヨ" ).replace("ラ","ラ" ).replace("リ","リ" ).replace("ル","ル" ).replace("レ","レ" ).replace("ロ","ロ" ).replace("ワ","ワ" ).replace("ヲ","ヲ" ).replace("ン","ン" ).replace("ァ","ァ" ).replace("ィ","ィ" ).replace("ゥ","ゥ" ).replace("ェ","ェ" ).replace("ォ","ォ" ).replace("ヵ","カ" ).replace("ヶ","ケ" ).replace("ッ","ッ" ).replace("ャ","ャ" ).replace("ュ","ュ" ).replace("ョ","ョ" ).replace("ヮ","ワ" ).replace("ヴ","ヴ" ).replace("ガ","ガ" ).replace("ギ","ギ" ).replace("グ","グ" ).replace("ゲ","ゲ" ).replace("ゴ","ゴ" ).replace("ザ","ザ" ).replace("ジ","ジ" ).replace("ズ","ズ" ).replace("ゼ","ゼ" ).replace("ゾ","ゾ" ).replace("ダ","ダ" ).replace("ヂ","ヂ" ).replace("ヅ","ヅ" ).replace("デ","デ" ).replace("ド","ド" ).replace("バ","バ" ).replace("ビ","ビ" ).replace("ブ","ブ" ).replace("ベ","ベ" ).replace("ボ","ボ" ).replace("パ","パ" ).replace("ピ","ピ" ).replace("プ","プ" ).replace("ペ","ペ" ).replace("ポ","ポ" )
    //記号の変換
    str = str.replace("!","!" ).replace("#","#" ).replace("$","$" ).replace("%","%" ).replace("&","&" ).replace("(","(" ).replace(")",")" ).replace("ー","ー" ).replace("-","-" ).replace("=","=" ).replace("^","^" ).replace("~","~" ).replace("|","|" ).replace("@","@" ).replace("‘","`" ).replace("「","「" ).replace("[","[" ).replace("{","{" ).replace("+","+" ).replace(":",":" ).replace("*","*" ).replace("」","」" ).replace("]","]" ).replace("}","}" ).replace("、","、" ).replace(",","," ).replace("<","<" ).replace("。","。" ).replace(".","." ).replace(">",">" ).replace("・","・" ).replace("/","/" ).replace("?","?" ).replace("_","_" )
    
    return str 
}

 

実行例

println(convertIntoHalfFromFull("ABCDEfghiアイウ123!#$%"))  --//ABCDEfghiアイウ123!#$%


説明

半角から全角へ置換する

 

引数

引数1(target:String):置換対象の文字列

 

返り値

置換後の文字列

 

コード

/*
=========================================================
[概  要] 半角から全角へ置換する
[引  数] target        : 置換対象の文字列
[戻り値] 置換後の文字列
[備  考]
=========================================================
*/
fun convertIntoFullFromHalf(target : String ):String
{
    var str : String

    //アルファベット(大文字)の変換
    str = target.replace("A","A" ).replace("B","B" ).replace("C","C" ).replace("D","D" ).replace("E","E" ).replace("F","F" ).replace("G","G" ).replace("H","H" ).replace("I","I" ).replace("J","J" ).replace("K","K" ).replace("L","L" ).replace("M","M" ).replace("N","N" ).replace("O","O" ).replace("P","P" ).replace("Q","Q" ).replace("R","R" ).replace("S","S" ).replace("T","T" ).replace("U","U" ).replace("V","V" ).replace("W","W" ).replace("X","X" ).replace("Y","Y" ).replace("Z","Z" )
    //アルファベット(小文字)の変換
    str = str.replace("a","a" ).replace("b","b" ).replace("c","c" ).replace("d","d" ).replace("e","e" ).replace("f","f" ).replace("g","g" ).replace("h","h" ).replace("i","i" ).replace("j","j" ).replace("k","k" ).replace("l","l" ).replace("m","m" ).replace("n","n" ).replace("o","o" ).replace("p","p" ).replace("q","q" ).replace("r","r" ).replace("s","s" ).replace("t","t" ).replace("u","u" ).replace("v","v" ).replace("w","w" ).replace("x","x" ).replace("y","y" ).replace("z","z" )
    //数値の変換
    str = str.replace("0","0" ).replace("1","1" ).replace("2","2" ).replace("3","3" ).replace("4","4" ).replace("5","5" ).replace("6","6" ).replace("7","7" ).replace("8","8" ).replace("9","9" )
    //カタカナの変換
    str = str.replace("ア","ア" ).replace("イ","イ" ).replace("ウ","ウ" ).replace("エ","エ" ).replace("オ","オ" ).replace("カ","カ" ).replace("キ","キ" ).replace("ク","ク" ).replace("ケ","ケ" ).replace("コ","コ" ).replace("サ","サ" ).replace("シ","シ" ).replace("ス","ス" ).replace("セ","セ" ).replace("ソ","ソ" ).replace("タ","タ" ).replace("チ","チ" ).replace("ツ","ツ" ).replace("テ","テ" ).replace("ト","ト" ).replace("ナ","ナ" ).replace("ニ","ニ" ).replace("ヌ","ヌ" ).replace("ネ","ネ" ).replace("ノ","ノ" ).replace("ハ","ハ" ).replace("ヒ","ヒ" ).replace("フ","フ" ).replace("ヘ","ヘ" ).replace("ホ","ホ" ).replace("マ","マ" ).replace("ミ","ミ" ).replace("ム","ム" ).replace("メ","メ" ).replace("モ","モ" ).replace("ヤ","ヤ" ).replace("ユ","ユ" ).replace("ヨ","ヨ" ).replace("ラ","ラ" ).replace("リ","リ" ).replace("ル","ル" ).replace("レ","レ" ).replace("ロ","ロ" ).replace("ワ","ワ" ).replace("ヲ","ヲ" ).replace("ン","ン" ).replace("ァ","ァ" ).replace("ィ","ィ" ).replace("ゥ","ゥ" ).replace("ェ","ェ" ).replace("ォ","ォ" ).replace("カ","ヵ" ).replace("ケ","ヶ" ).replace("ッ","ッ" ).replace("ャ","ャ" ).replace("ュ","ュ" ).replace("ョ","ョ" ).replace("ワ","ヮ" ).replace("ヴ","ヴ" ).replace("ガ","ガ" ).replace("ギ","ギ" ).replace("グ","グ" ).replace("ゲ","ゲ" ).replace("ゴ","ゴ" ).replace("ザ","ザ" ).replace("ジ","ジ" ).replace("ズ","ズ" ).replace("ゼ","ゼ" ).replace("ゾ","ゾ" ).replace("ダ","ダ" ).replace("ヂ","ヂ" ).replace("ヅ","ヅ" ).replace("デ","デ" ).replace("ド","ド" ).replace("バ","バ" ).replace("ビ","ビ" ).replace("ブ","ブ" ).replace("ベ","ベ" ).replace("ボ","ボ" ).replace("パ","パ" ).replace("ピ","ピ" ).replace("プ","プ" ).replace("ペ","ペ" ).replace("ポ","ポ" )
    //記号の変換
    str = str.replace("!","!" ).replace("#","#" ).replace("$","$" ).replace("%","%" ).replace("&","&" ).replace("(","(" ).replace(")",")" ).replace("ー","ー" ).replace("-","-" ).replace("=","=" ).replace("^","^" ).replace("~","~" ).replace("|","|" ).replace("@","@" ).replace("`","‘" ).replace("「","「" ).replace("[","[" ).replace("{","{" ).replace("+","+" ).replace(":",":" ).replace("*","*" ).replace("」","」" ).replace("]","]" ).replace("}","}" ).replace("、","、" ).replace(",","," ).replace("<","<" ).replace("。","。" ).replace(".","." ).replace(">",">" ).replace("・","・" ).replace("/","/" ).replace("?","?" ).replace("_","_" )

    return str 
}

 

実行例

println(convertIntoFullFromHalf("ABCDEfghiアイウ123!#$%"))  --ABCDEfghiアイウ123!#$%


説明

文字列を右から〇文字取得する

 

引数

引数1(target:String):この文字列から切り取る

引数2(num:Int)     :切り取り文字数

 

返り値

取得した文字列

 

使用する構文

"この文字から取得する".substring(開始位置,終了位置)

開始位置は0始まり

終了位置は取得文字数ではなく、〇文字目まで取得する(〇文字目は含まない)

 

コード

/*
=========================================================
[概  要] 文字列を右から〇文字取得する
[引  数] target        : この文字列から切り取る
[引  数] num           : 切り取り文字数
[戻り値] 取得後の文字列
[備  考] 
=========================================================
 */
 fun right(target : String , num : Int):String
 {
     return target.substring(target.length - num, target.length)
 }

 

実行例

println(right("abcdefghijkmn",5)) //ijkmn

説明

文字列から〇文字目から〇文字取得する。

 

引数

引数1(target:String):この文字列から切り取る

引数2(start:Int)         :切り取り開始位置(1からカウント)

引数3(num:Int)       :切り取り文字数(この文字数を取得する)

 

返り値

取得した文字列

 

使用する構文

"この文字から取得する".substring(開始位置,終了位置)

開始位置は0始まり

終了位置は取得文字数ではなく、〇文字目まで取得する(〇文字目は含まない)

 

コード

/*
=========================================================
[概  要] 文字列から〇文字目から〇文字取得する。
[引  数] target        : この文字列から切り取る
[引  数] start         : 切り取り開始位置(1から始まり)
[引  数] num           : 切り取り文字数
[戻り値] 取得後の文字列
[備  考] startからのnumが文字数を超える場合、
         startからすべての文字列を返す
=========================================================
*/
fun mid(target : String , start : Int , num : Int):String
{

    if ((start - 1) < 0){ return "" }else if( (start - 1) + num > target.length){
        return target.substring((start - 1))
    }

    return target.substring((start - 1) , (start - 1) + num)
}

実行例

println(mid("abcdefghijklm",1,5)) //abcde
println(mid("abcdefghijklm",3,4)) //cdef

説明

文字列に特定の文字列が含まれるかチェックします。

 

引数

引数1(target:String):対象の文字列

引数2(findString:String):探す文字列

引数3(trimFlg:Boolean):文字列にtrimをつけるかのフラグ

※引数3のフラグ:True=Trimあり、False=Trimなし、Default=False

 

返り値

True:含む False:含まれない

 

使用する構文

Regex("この文字を探す").containsMatchIn("この文字から探す")

 

コード

/*
=========================================================
[概  要] 文字列内に指定された文字列があるか調べる
[引  数] target        : この文字列から探す
[引  数] findString    : 探す文字列
[引  数] trimFlg       : trimをつけるかのフラグ
                       (True:Trimつける、False:Trimつけない、Default:False)
[戻り値] True:ある、False:ない
[備  考]
========================================================="
 */
fun checkCharExists(target : String , findString : String , trimFlg : Boolean=false) : Boolean
{

    if(trimFlg == true){
        //trimあり
        return Regex(findString.trim()).containsMatchIn(target.trim())
    }else{
        //trimなし
        return Regex(findString).containsMatchIn(target)
    }

}

 

実行例

println(checkCharExists("abcde","cd")) // true 
println(checkCharExists("abcde","ef")) // false