在 Smarty 中执行此操作
{math equation=1.1+0.7 format="%.2f"}
Run Code Online (Sandbox Code Playgroud)
我得到的结果是逗号分隔的十进制 1,80
不知道如何获得 1.80 值。
非常感谢。
这是我的代码:
import java.text.DecimalFormat;
public class asd {
public static void main(String args[]) {
DecimalFormat d = new DecimalFormat("#00.00");
System.out.println(d.format(0250));
}
}
Run Code Online (Sandbox Code Playgroud)
它给出168.00了答案.这不是我期待的答案(250.00).为什么会这样?
我已经为我正在使用的双精度声明了变量:
z= 345.876;
Run Code Online (Sandbox Code Playgroud)
现在我想显示小数点之前的位数。我尝试执行以下操作:
String string_form = new Double(z).toString().subString(0,string_form.indexOf('.'));
double t = Double.valueOf(string_form);
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:“方法 subString(int, int) 对于 String 类型未定义”
然后快速修复显示将其小写更改为:子字符串。然而,错误随后更改为字符串“string_form”,表示它尚未初始化。关于该做什么有什么想法吗?
我该如何修改它以查找数字后面的位数?我知道在这部分
.indexOf('.')
Run Code Online (Sandbox Code Playgroud)
我会用数字替换小数点,但我该如何更改它,以便它显示数字后面而不是前面有多少位数字?谢谢。是的,我已经导入了十进制格式文本语言。
我正在使用 DecimalFormat 使用#.#
产生输出的模式从我的输出中删除尾随零
12.5 --> 12.5
12 --> 12
但是我需要让它产生
12.5 --> 12.5
12 --> 12.0
这怎么会发生?
我有以下形式的打印声明:
print("a = ", a, "b = ", b, "c = ", c)
Run Code Online (Sandbox Code Playgroud)
其中 a、b 和 c 是浮点数。我想打印到小数点后三位,并保留打印语句当前的形式,如果可能的话?
试图
在类似的帖子之后,我尝试了以下操作:
print(" %.3f a = ", a, "%.3f b = ", b, "%.3f c = ", c)
Run Code Online (Sandbox Code Playgroud)
但这只是在打印语句中打印了“%.3f”。关于如何调整我的打印报表有什么建议吗?
我需要这样的格式数字:
23.0 -> 23
23.20 -> 23.20
23.00 -> 23
23.11 -> 23.11
23.2 -> 23.20
23.999 -> 24
23.001 -> 23
1345.999 -> 1346 // Edited the question to add this from the OP's comment
Run Code Online (Sandbox Code Playgroud)
这是我的代码:java:
public static String toPrice(double number) {
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
formatSymbols.setGroupingSeparator(' ');
DecimalFormat format = new DecimalFormat("#,###,###.##", formatSymbols);
return format.format(number);
}
Run Code Online (Sandbox Code Playgroud)
科特林:
fun Double.toPrice(): String = DecimalFormat("#,###,###.##", DecimalFormatSymbols().apply {
groupingSeparator = ' '
}).format(this)
Run Code Online (Sandbox Code Playgroud)
但对于输入 23.20 或 23.2,我得到结果 23.2。这对我来说是错误的。我需要 23.20。我应该使用哪种字符串模式来实现此结果?请帮我。
为什么结果不同?如果我使用浮动它得到,675如果我使用双我得到,674 ...不是很奇怪?
float f = 12345.6745;
double d = 12345.6745;
Locale l = Locale.forLanguageTag("es-ES");
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(l);
print(df.format(f));
>> 12.345,675
l = Locale.forLanguageTag("es-ES");
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(l);
print(df.format(d));
>> 12.345,674
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在使用此代码:
DecimalFormat df = new DecimalFormat();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
float a=(float) 15000.345;
Sytem.out.println(df.format(a));
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:15,000.35
我不希望逗号进入此输出.我的输出应该是:15000.35.
在Java中获取此输出的最佳方法是什么?
有什么方法可以在双变量中存储多达6位小数?例如,考虑一个值为1.0的双变量,但我希望存储的值为1.000000
更新: 其实我接受了mettl的评估测试.我被要求编写一个程序来计算平均值,中位数和模式.结果应存储在double类型的输出变量S中,即output1,output2和output3.我也被要求用性别小数位存储它.因为我不能使用print语句,所以DecimalFormat是没用的.
我需要将格式为"101.46"的任何值转换为十进制.
string s = "101.46";
decimal value = Convert.ToDecimal(s);
Run Code Online (Sandbox Code Playgroud)
但小数最终值始终为10146.我需要输出值的格式为十进制的101.46
decimalformat ×10
java ×7
double ×2
c# ×1
kotlin ×1
numbers ×1
precision ×1
python ×1
python-3.x ×1
smarty3 ×1