不要四舍五入,
如果没有小数位,将小数点后面的.0改成.00(比如2转换成2.00,而不是2.0)
不知道会有多少位小数,有多少位显示多少位
比如这种的
88.495000
将后面的0给去掉
要是88.495236
就不用去掉
如果是六个0就保留小数点后两位
比如
100.000000
改成
100.00
这是C++实现代码,你可以参考一下
#include <iostream>
#include <algorithm>
using namespace std;
string truncate_tail_zeros(const string &str)
{
if (str.find_first_of('.') == string::npos)
return str + ".00";
string r;
auto itr = find_if(str.rbegin(), str.rend(), [](auto c)
{ return c != '0'; });
reverse_copy(itr, str.rend(), back_inserter(r));
if (*itr == '.')
r += "00";
return r;
}
int main()
{
string str;
while (cin >> str)
cout << "=> " << truncate_tail_zeros(str) << endl;
return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
100
=> 100.00
100.0
=> 100.00
100.000
=> 100.00
100.0012345600
=> 100.00123456
class Test{
public static String getFormatNumber(Double a){
BigDecimal bigDecimal = new BigDecimal(new DecimalFormat("##.00").format(a));
if(a == bigDecimal.doubleValue()){
return bigDecimal.toString();
}
return a.toString();
}
public static void main(String[] args) {
System.out.println(getFormatNumber(2.00d));
System.out.println(getFormatNumber(2.12321300));
System.out.println(getFormatNumber(88.2324));
System.out.println(getFormatNumber(88.0000));
}
import java.util.Scanner;
public class test {
public static String work(Double num) {
String str = num.toString();
int flag = str.indexOf('.');
if (flag == -1)
return str + ".00";
else
str += "00";
int i = str.length() - 1;
while (i > flag + 2 && str.charAt(i) == '0')
i--;
return str.substring(0, i + 1);
}
public static void main(String[] args) {
Double t;
Scanner sc = new Scanner(System.in);
t = sc.nextDouble();
System.out.println(test.work(t));
sc.close();
}
}
import java.util.Scanner;
public class My {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Double shuju=sc.nextDouble();
String sj=shuju+"";
System.out.println(sj);
int num=sj.indexOf('.')+1;
String result=sj.substring(0, num);
System.out.println(result);
String temp="";
for(int i=sj.length()-1;i>=num;i--) {
if('0'==sj.charAt(i)) {
if(i-num==1) {
result+=sj.substring(num, i+1);
break;
}
}else {
result+=sj.substring(num, i+1);break;
}
}
System.out.println(result);
}
}
这个是对的
import java.util.Scanner;
public class My {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Double shuju=sc.nextDouble();
String sj=shuju+"00000";
int num=sj.indexOf('.')+1;
String result=sj.substring(0, num);
String temp="";
for(int i=sj.length()-1;i>=num;i--) {
if('0'==sj.charAt(i)) {
if(i-num==1) {
result+=sj.substring(num, i+1);
break;
}
}else {
result+=sj.substring(num, i+1);break;
}
}
System.out.println(result);
}
}
import java.text.DecimalFormat;
public class Test2 {
public static void main(String[] args) {
Double number = 46.017700; //这里修改数字
DecimalFormat dd = new DecimalFormat("#.000000");
String result = dd.format(number);
int d = getIndex(result);
System.out.println(deleteString(result,d));
}
public static int getIndex(String s){
int index = 999;
for (int i = s.length()-1; i > 0; i--) {
String lastNum = String.valueOf(s.charAt(i)); //从末尾开始
if (lastNum.equals("0")){ //末尾为0
index = Math.min(index,i); //记录最小index
}else if (lastNum.equals(".")){ //6位全是0
return -1;
}else if (!lastNum.equals("0")) {
return index;
}
}
return index;
}
public static String deleteString(String str, int index ){
if (index == 999){
return str;
}
if (index == -1){
double result = Double.parseDouble(str);
return new DecimalFormat("#.00").format(result);
}
String delStr = "";
for (int i = 0; i < str.length(); i++) {
if(i != index){
delStr += str.charAt(i);
}
else break;
}
return delStr;
}
}
这个题主一个问题重复问多次,是刷回答问题的?
都对
public static String wrok(String string){
int i = string.indexOf(".");//寻找小数点
if(i==-1){
//没找到,在后面加上.00直接返回
return string+=".00";
}else{
//找到了
if(string.substring(string.indexOf(".")+1,string.length()).equals("000000")){
//如果小数后面后面六个是0,那么就替换成00
string=string.replace("000000","00");
}else{
//如果最后一位小数是0,去掉零
while (string.lastIndexOf("0")==string.length()-1){
string=string.substring(0,string.lastIndexOf("0"));
}
}
}
//返回结果
return string;
}
加入这个方法,望采纳
public static void main(String[] args) {
Double value = 1155.905600;
DecimalFormat format = new DecimalFormat("0.00");
BigDecimal bigDecimal = new BigDecimal(format.format(value));
String output = "";
output = value.toString();
if (value == bigDecimal.doubleValue()) {
output = bigDecimal.toString();
}
System.out.println("输出: " + output);
}
楼上这么多代码,我就想问楼主想用什么语言实现