小编241*_*116的帖子

使用DecimalFormat获取不同数量的小数位

所以我想使用Decimal Format类来舍入数字:

double value = 10.555;

DecimalFormat fmt = new DecimalFormat ("0.##");

System.out.println(fmt.format(value));
Run Code Online (Sandbox Code Playgroud)

这里,变量value将四舍五入到2位小数,因为有两个#s.但是,我想要舍入value到未知的小数位数,由一个单独的整数表示numPlaces.有没有办法通过使用十进制格式化器来实现这一目标?

例如,如果numPlaces = 3value = 10.555,则value需要四舍五入到小数点后3位

java string decimalformat

4
推荐指数
2
解决办法
3026
查看次数

文档参考会增加阅读次数吗?

我有一个users收藏和一个rentals收藏。文档rental有一个用于租赁该项目的用户的字段,存储为文档引用 ( rentalDocumentSnapshot['renter'] = /users/<some id here>)。要查询您作为租户的所有租金,我只需传递到一个流中:

Firestore.instance.collection('rentals')
    .where('renter', isEqualTo: Firestore.instance.collection('users').document(myUserID))
    .snapshots()
Run Code Online (Sandbox Code Playgroud)

调用会Firestore.instance.collection('users').document(myUserID)创建读取吗?那么,将用户 ID 作为字符串而不是文档引用存储在租赁中会更好吗?

firebase flutter google-cloud-firestore

2
推荐指数
1
解决办法
289
查看次数

Kotlin:将当前日期存储在 Firestore 中作为时间戳

不知道为什么我找不到我要找的东西。但我只想将当前时间存储到 Firestore 文档中,如下所示:

在此输入图像描述

。在 Flutter 中我们只是这样做:

'timestamp': DateTime.now()
Run Code Online (Sandbox Code Playgroud)

我尝试在 kotlin 中这样做:

"timestamp" to LocalDateTime.now()
Run Code Online (Sandbox Code Playgroud)

但它给了我一些复杂的领域:

在此输入图像描述

kotlin google-cloud-firestore

2
推荐指数
1
解决办法
2504
查看次数

Kotlin 协程和 Android - 获取 Firestore 查询任务结果的结果

我正在编写一个函数,该函数对 firestore 文档进行查询,并将它们返回到列表中。如果没有文档,则返回 null(或空列表)。我对 Kotlin 不太熟悉,我只知道 Flutter

Kotlin 代码(我尝试过),但它说我无法在侦听器内返回:

fun getPlaces(type: String): List<DocumentSnapshot>? {
        db.collection("users")
            .whereEqualTo("type", type)
            .get()
            .addOnSuccessListener { documents ->
                if (documents.isEmpty) {
                    return null
                } else {
                    return documents
                }
            }
            .addOnFailureListener { exception ->
                Log.w(TAG, "Error getting documents: ", exception)
            }
    }
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试做的 Flutter 等效项:

List<DocumentSnapshot> getDocs(String type) async {
    QuerySnapshot snaps = await db.collection('users').where('type', isEqualTo: type).getDocuments();

    if (snaps.documents.isEmpty) {
        return null;
    } else {
        return snaps.documents;
    }
}
Run Code Online (Sandbox Code Playgroud)

语法可能有点不对,但类似的东西

android kotlin google-cloud-firestore

1
推荐指数
1
解决办法
2252
查看次数

麻烦读下一个int

(原始问题,已经回答)

我想要一个程序从文本文件中读取整数.数据文件如下所示:

5 4
3 5
4 5
3 2
Run Code Online (Sandbox Code Playgroud)

每行的第一个数字是par,第二个数字是分数.因为5 - 4 = 1,得分低于标准杆1(这是一只小鸟).我的第一行代码:

    in = new Scanner (dataFile);
    System.out.println("Golf Scores: ");                                                
    String line1 = in.nextLine();       
    System.out.println("Score 1: " + line1);
    int par1 = in.nextInt();
    System.out.println("Par is: " + par1);
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到了这个:

高尔夫球得分:

得1:5 4

标准是:3

所以标准杆和得分正确显示.但是,显示的参数显示文本文件中下一行的参数.我希望它再次显示"5".我尝试将in.nextInt放在in.nextLine之前,但是当我尝试时,我得到了这个

高尔夫球得分:

得1:4

标准是:5

如果我需要添加任何内容来更好地解释我的问题,请告诉我.

java

0
推荐指数
1
解决办法
230
查看次数