我从quandl中提取数据并进行一些计算。问题是结果最多可达15个小数点。有什么方法可以将这个限制为两个?
public class MainActivity extends AppCompatActivity {
VideoAdapter videoAdapter;
private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount, totalItemCount;
int limit=0;
boolean loadmore=false;
ArrayList<Video> testParseModels=new ArrayList<>();
RecyclerView recyclerView;
LinearLayoutManager linearLayoutManager;
ParseQuery<ParseObject> parseQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
Parse.initialize(new
dataParse();
/*parseQuery.setLimit(recyclerView.getChildCount()+5);
if(recyclerView.getChildCount()>5){
parseQuery.setSkip(5);
}*/
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(loadmore==true){
parseQuery.setSkip(limit);
parseQuery.setLimit(5);
}else{
parseQuery.setLimit(5);
}
dataParse();
}
});
videoAdapter = new VideoAdapter(testParseModels, MainActivity.this);
linearLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(videoAdapter);
/*if(loadmore==true)
{
parseQuery.setSkip(limit);
parseQuery.setLimit(5);
}
else
{
parseQuery.setLimit(5);
}*/
}
public void dataParse(){
parseQuery=ParseQuery.getQuery("Video");
parseQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
/* limit = limit+ objects.size();
if(objects.size()==0)
{
loadmore = false;
}
else
{
loadmore = true;
}*/
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
Video video = new Video();
video.setCityname(objects.get(i).getString("cityname"));
video.setName(objects.get(i).getString("name"));
video.setStorageId(objects.get(i).getString("storageId"));
video.setS3link(objects.get(i).getString("s3link"));
//Log.d("s3link", objects.get(i).getString("cityname"));
testParseModels.add(video);
}
} else {
Toast.makeText(getApplicationContext(), "some error", Toast.LENGTH_LONG).show();
}
// videoAdapter.notifyDataSetChanged();
}
});
}
}
答案 0 :(得分:0)
试试这个。
// ((data1.data[0][4]-data1.data[29][4])*100/data1.data[0][4]).toFixed(2)
$('#value1').html(((data1.data[0][4]-data1.data[29][4])*100/data1.data[0][4]).toFixed(2));
这会将你的小数点从任意数量的点减少到2。
答案 1 :(得分:0)
使用numObj.toFixed([digits])
进行回合。
toFixed()返回numObj的字符串表示形式,该表示形式不使用指数表示法,并且在小数位后面具有正确的位数。如有必要,数字将四舍五入,如果需要,小数部分用零填充,以使其具有指定的长度。如果numObj大于1e + 21,则此方法只调用Number.prototype.toString()并以指数表示法返回一个字符串。
实施例
var numObj = 12345.6789;
numObj.toFixed(); // Returns '12346': note rounding, no fractional part
numObj.toFixed(1); // Returns '12345.7': note rounding
numObj.toFixed(6); // Returns '12345.678900': note added zeros
(1.23e+20).toFixed(2); // Returns '123000000000000000000.00'
(1.23e-10).toFixed(2); // Returns '0.00'
2.34.toFixed(1); // Returns '2.3'
2.35.toFixed(1); // Returns '2.4'. Note that it rounds up in this case.
-2.34.toFixed(1); // Returns -2.3 (due to operator precedence, negative number literals don't return a string...)
(-2.34).toFixed(1); // Returns '-2.3' (...unless you use parentheses)