Swift:NSLengthFormatter只有1个小数位数

时间:2015-12-18 09:12:37

标签: swift digits nsnumberformatter

我使用NSLengthFormatter显示正确的实体,如米,脚,码等 该方法有效,但我没有得到正确数量的小数位数。

public class MainActivity extends Activity 
{
VideoView videoview;
ProgressBar progressBar;
Context context;
String VideoURL = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btn =  (Button) findViewById(R.id.mainButton);
    btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            plyvido();

        }
    });

}
//@Override
public void plyvido(){
    final Dialog dialog = new Dialog(MainActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.videodialog);
    dialog.show();
    MediaController mediacontroller = new MediaController(
        MainActivity.this);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    lp.copyFrom(dialog.getWindow().getAttributes());

    dialog.getWindow().setAttributes(lp);

    videoview = (VideoView) dialog.findViewById(R.id.videodialogVideoView);
    progressBar = (ProgressBar) dialog.findViewById(R.id.progressbar);
    mediacontroller.setAnchorView(videoview);
    Uri uri = Uri.parse(VideoURL);
    videoview.setZOrderOnTop(true);
    videoview.setVideoURI(uri);
    videoview.setMediaController(mediacontroller);
    videoview.start();



    progressBar.setVisibility(View.VISIBLE);
    videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.start();
                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                        @Override
                        public void onVideoSizeChanged(MediaPlayer mp, int arg1,
                                                       int arg2) {
                            // TODO Auto-generated method stub
                            progressBar.setVisibility(View.GONE);
                            mp.start();
                        }
                    });

            }
        });


}


}

我用Google搜索,但我找不到如何配置NSLengthFormatter。 NSNumberFormatter具有let distance = getDistanceAsDouble() //636.62084358567 in meters let format = String(format: "%.1f",distance) //636.6 if let formattedDouble = Double(format) { let lengthFormatter = NSLengthFormatter() let formattedDistance = lengthFormatter.stringFromMeters(formattedDouble) lbl.text = "\(formattedDistance)" // printed: 697.936 yd } numberStyle等属性,但NSLengthFormatter没有...或?

我尝试将maximumFractionDigitis转换为Double,我可以再次使用formattedDistance。这不起作用,因为String(format: ""...包含“yd”(或其他实体)。

如何解决此问题?

1 个答案:

答案 0 :(得分:6)

NSLengthFormatter有一个属性numberFormatter: NSNumberFormatter,所以用它来设置它,就像这样...

if let formattedDouble = Double(format) {
    let lengthFormatter = NSLengthFormatter()
    lengthFormatter.numberFormatter.maximumFractionDigits = 1 // *****
    let formattedDistance = lengthFormatter.stringFromMeters(formattedDouble)
    print("\(formattedDistance)") 
}