我已经在评论中提出了这个问题:
我正在尝试将 double 转换为具有固定小数位数的字符串。在上面的问题中,解决方案非常简单。使用时
String.format("%.4g", 0.1234712)
Run Code Online (Sandbox Code Playgroud)
我得到了预期的结果,一个四舍五入的数字:
0.1235
Run Code Online (Sandbox Code Playgroud)
但是当小数点后有零时:
String.format("%.4g", 0.000987654321)
Run Code Online (Sandbox Code Playgroud)
这将返回:
0,0009877
Run Code Online (Sandbox Code Playgroud)
看起来该函数忽略了数字中的前导零。
我知道我可以定义一个新的 DecimalFormat 但我想了解这个问题。并了解一些语法。
为此我尝试过很多事情,但我就是不知道如何让它发挥作用。一般来说,我对编码很陌生,所以可能缺乏理解!简而言之,我有一个包含一定数量元素的数组,并且我希望能够对从特定索引开始到特定索引结束的连续元素求和。
const array = [1,2,3,4,5,6]
Run Code Online (Sandbox Code Playgroud)
我希望能够对索引 1 和 3 求和,在本例中为 2+4 =6。
有没有办法使用array.filter()
或仅使用我根据 求和的函数array.indexof()
?
数组列表实现的问题:
我的代码
List<Integer> arrayList=new ArrayList<Integer>(3);
arrayList=Arrays.asList(10,20);
System.out.println(arrayList.size());
//arrayList.add(30);
System.out.println(arrayList.size());
Run Code Online (Sandbox Code Playgroud)
我正进入(状态 unsupportedException at line 4
有什么问题?
我用 HTML、CSS 和Bootstrap创建了一个简单的表格,我想更改单元格中的日期。(翻译文本)
<table class="table table-striped" id="TabelPret">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">service</th>
<th scope="col">price(Euro)</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>consulting</td>
<td>50</td>
</tr>
<tr>
<th scope="row">2</th>
<td>RECONSULT</td>
<td>15</td>
</tr>
<tr>
<th scope="row">3</th>
<td>1 procedur/30 min</td>
<td>10</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
现在对于 JS,我尝试选择表然后添加新行和列:
var array = [
["a1", "b1", "c1"],
["a2", "b1", "c1"],
["a3", "b1", "c1"],
["a4", "b1", "c1"],
["a5", "b1", "c1"],
["a6", "b1", "c1"],
["a7", "b1", "c1"]
];
Run Code Online (Sandbox Code Playgroud)
该数组将是新单元格,因此(a1
翻译为 id,b1
翻译为咨询,c1
翻译为价格......等)
table = …
Run Code Online (Sandbox Code Playgroud) 我目前正在编写一段使用 base 36 编码的 JavaScript。
我遇到了这个问题:
parseInt("welcomeback",36).toString(36)
Run Code Online (Sandbox Code Playgroud)
看来要回来了"welcomebacg"
。
我在 Chrome 开发人员的控制台和 Node.js 中对此进行了测试,结果相同。
这个结果有什么合乎逻辑的解释吗?
我试图将句子中第一个单词的第一个字母大写。
这是 tsx 文件中的数据 { downloadPriceHistory
this.text ({ id: , defaultMessage: 'Download Price History' }) } 上面显示的 id 来自数据库,它可以以各种形式发送到 api。
我试图在下面使用这个逻辑:
export function titleCase(string) {
string = 'hello World';
const sentence = string.toLowerCase().split('');
for (let i = 0; i < sentence.length; i++) {
sentence[i] = sentence[i][0].toUpperCase() + sentence[i];
}
return sentence;
}
Run Code Online (Sandbox Code Playgroud)
例如,对于 input "Download Price History"
,结果应该是"Download price history"
。
尝试使用 Javascript 插入 css 文档,但是我收到错误消息,提示必须启用 CORS。有办法处理吗?
这是代码:
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://use.fontawesome.com/releases/v5.2.0/css/all.css';
link.integrity = 'sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ';
link.crossorigin = 'anonymous';
document.head.appendChild(link);
Run Code Online (Sandbox Code Playgroud) 我想将一个对象作为参数传递给 onclick 函数。然后我将该参数调用到函数中并推入空数组。但我尝试了一切,它只是推送 id、名称等。我想将laundryValue[i]
整个数组onclick="getLaundryClick()
作为参数传递。
var laundryValue = [
{id: 1, name: 'Sunglasses', price: 25},
{id: 2, name: 'Jeans', price: 10},
{id: 3, name: 'Shirts', price: 15},
{id: 4, name: 'Cables', price: 20}
]
var newLaundryValue = [];
for (var i in laundryValue) {
var wrap = laundryValue[i];
document.getElementById('laundry-value').innerHTML +=
'<li>' +
'<div class="laundry-name">' + laundryValue[i].name + '</div>' +
'<div class="laundry-price">' + laundryValue[i].price + '</div>' +
'<button class="laundry-btn" onclick="getLaundryClick(' + [wrap] + ')">' + 'Add' + '</button>' …
Run Code Online (Sandbox Code Playgroud)for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
String[] bin = new String[n];
for (int i = 0; i < n; i++) {
bin[i] = Integer.toBinaryString(arr[i]);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码将整个整数数组转换为String
s数组(包含输入字符串的二进制格式),但有一个警告。
例如:
如果输入数组是:2 3 7 10
二进制字符串数组将是:10
11
111
1010
但我希望输出数组如下所示:
0010
0011
0111
1010
#2
如果输入数组是:2 10 20
二进制字符串数组将是:
10
1010
10100
但我希望输出数组如下所示:
00010
01010
10100
为什么这有效:
String a = null;
String b = a != null && a.equals("Nan") ? "Nan" : a;
System.out.println(b);
Run Code Online (Sandbox Code Playgroud)
但这会产生 NPE:
Double value = null;
Double v = value != null && value.isNaN() ? 0.0 : value;
System.out.println(v);
Run Code Online (Sandbox Code Playgroud)
将其重写为:
Double value = null;
Double v;
if (value != null && value.isNaN()) {
v = 0.0;
} else {
v = value;
}
Run Code Online (Sandbox Code Playgroud)
当然可以按预期工作。但是为什么我在使用时使用三元/条件运算符得到NPE Double
,而在使用时没有NPE String
?我缺少什么?
javascript ×6
java ×4
node.js ×2
arrays ×1
base ×1
base36 ×1
binary ×1
bootstrap-4 ×1
collections ×1
comparison ×1
cors ×1
css ×1
discord ×1
discord.js ×1
html ×1
integer ×1
reactjs ×1
rounding ×1
string ×1
tags ×1