在Kotlin中使用十进制格式格式化数字

时间:2018-12-19 09:30:39

标签: android kotlin decimalformat

我遇到一个问题,我需要用例如 5000,00 的数字乘以( 1,025 ^ 3 )来进行一些计算。

因此,在这种情况下, Sub Insert_10_columns() Columns("B:K").Insert Shift:=xlToRight, _ CopyOrigin:=xlFormatFromLeftOrAbove For i = 2 To 11 ActiveSheet.Cells(1, i).Value = "Option " & i - 1 Next i End Sub Sub Look_For_Text() Dim LastRow As Long LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row For i = 2 To LastRow + 1 For k = 1 To 10 If InStr(1, (Cells(i, 1).Value), "Option " & k) > 0 Then ActiveSheet.Cells(i, k + 1).Value = "Option " & k End If Next k Next i End Sub

所以我的问题是,如何使用小数格式将数字5385,45格式化为 5.385,45

我自己尝试过,我完成了这段代码,在应用程序中输出5385,45,但没有输出 5.385,45

import java.util.Arrays;

public class SmallPositiveInteger {

     public int solution(int[] A) {

        //filter only +ve elements
        int[] B = Arrays.stream(A).filter(value -> value > 0).toArray();
        if (B.length == 0)   //If no +ve number return 1
            return 1;

        //get the distinct of it
        int[] C = Arrays.stream(B).sorted().distinct().toArray();
        int N = C.length;

        if (C[0] != 1)  //if zero element is not zero it is 1
            return 1;

        //leaves us with elements to check the sequence
        for (int i = 1; i < N; i++) {

            if (C[i] - C[i - 1] > 1)
                return C[i - 1] + 1;

        }
        //if nothing found ,increment last element by 1
        return C[N - 1] + 1;

4 个答案:

答案 0 :(得分:2)

这是您需要的格式:

this._super(...arguments);

将打印:

 repositories {
        maven {
            url = 'http://nexus.something.com/repository/maven-central/'
        }
        maven {
            url = 'http://nexus.something.com/repository/maven-releases/'
        }
        maven {
            url = 'http://nexus.something.com/repository/maven-snapshots/'
        }
    }

如果您始终需要精确到小数点后两位:

val dec = DecimalFormat("#,###.##")

答案 1 :(得分:1)

val num = 1.34567
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING

println(df.format(num))

运行程序时,输出为: 1.34

检查: https://www.programiz.com/kotlin-programming/examples/round-number-decimal

答案 2 :(得分:0)

尝试V.bounds.height 。有关DecimalFormat的示例,请检查此link

答案 3 :(得分:0)

使用过:

%.numberf

fun main(args: Array<String>) {
var A: Double
A = readLine()!!.toDouble()
var bla = A*A
var calculator = 3.14159 * bla
println("A=%.4f".format(calculator))
}