如何强制将小数格式化为两位数?

时间:2016-06-16 06:50:03

标签: ruby format

我正在试图弄清楚如何将数字格式设置为29.9,例如29.90number = 29.90 #=> 29.9 number.to_s #=> "29.9" number.round(2).to_s #=> "29.9" 。到目前为止我试过这个:

"29.90"

我应该使用什么方法来获取public class MonFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS) == true) { explain(); } else { //demander l'autorisation } } else { View rootView = inflater.inflate(R.layout.fragment_gestion_contact, container, false); final ListView list = (ListView) rootView.findViewById(android.R.id.list); final List<String> contacts = retrieveContacts(getActivity().getContentResolver()); if (contacts != null) { list.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, contacts)); } return rootView; } } else { View rootView = inflater.inflate(R.layout.fragment_gestion_contact, container, false); final ListView list = (ListView) rootView.findViewById(android.R.id.list); final List<String> contacts = retrieveContacts(getActivity().getContentResolver()); if (contacts != null) { list.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, contacts)); } return rootView; } return null; } private List<String> retrieveContacts(ContentResolver contentResolver) { final Set<String> contacts = new HashSet<String>(); final Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, new String[] { ContactsContract.Data.DISPLAY_NAME, ContactsContract.Data._ID, ContactsContract.Contacts.HAS_PHONE_NUMBER }, null, null, null); if (cursor == null) { Log.e("retrieveContacts", "Cannot retrieve the contacts"); return null; } if (cursor.moveToFirst() == true) { do { final long id = Long.parseLong(cursor.getString(cursor.getColumnIndex(ContactsContract.Data._ID))); final String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); final int hasPhoneNumber = cursor.getInt(cursor.getColumnIndex(ContactsContract.Data.HAS_PHONE_NUMBER)); if (hasPhoneNumber > 0) { contacts.add(name); } } while (cursor.moveToNext() == true); } if (cursor.isClosed() == false) { cursor.close(); } final List<String> sortedContacts = new ArrayList<String>(contacts); Collections.sort(sortedContacts); return sortedContacts; } private void askForPermission() { requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 2); } private void explain() { askForPermission(); }

2 个答案:

答案 0 :(得分:1)

<div class="container">
  <div class="right">
    <p><b>Note: </b>When aligning using the position property, always include the !DOCTYPE declaration! If missing, it can produce strange results in IE browsers.</p>
  </div>
  <div class="clearfix"></div>
</div>

感谢Gourav Naik

答案 1 :(得分:0)

这适合你吗?

printf( "%.02f", 22.9 )