Swift 2 - 将十进制0添加到CGFloat

时间:2016-03-07 13:37:25

标签: swift2 decimalformat cgfloat

我正在尝试将字符串转换为CGFloat个数字。 如果等于0,我的代码会删除最后一位数字。

如何防止删除零?

let str = "17.30"

let flt = CGFloat((str as NSString).doubleValue) + 2.0

print(flt) // 19.3 -- should be 19.30

3 个答案:

答案 0 :(得分:4)

CGFloat无法做到这一点,将浮动转换回String

print(String(format: "%.2f", flt)) // 19.30

答案 1 :(得分:1)

<body class="preload"> <!-- ======================================Background Slider Image====================================== --> <div id="kenburns-bg"></div> <!-- ======================================END Background Slider Image================================== --> <!-- ======================================Let's through down an Overlay================================ --> <div id="firefly" class="bg-overlay"> <canvas id="star-canvas"></canvas> </div> <!-- ======================================That's it, now lets End Overlay============================== --> <!-- =====================================Header======================================================= --> <header> <div class="intro"> <!-- ========== This is were the magic happens ========== --> <div id="popup-1" class="rbModal"> <div class="window"> <!-- popup content --> <iframe id="myVideo" src="http://www.youtube.com/embed/vsObnzivL1A?&rel=0&loop=1&showinfo=0&disablekb=1&modestbranding=1&controls=0&hd=1&autohide=1&color=white&wmode=opaque" frameborder="0" allowfullscreen> </iframe> <!-- / popup content --> </div> </div> <!-- ========== Wasn't that cool? ========== --> <!-- ====================================Show my Awesome Site Logo==================================== --> <div class="logo"><img src="images/RB logo-horizantal-shadowdrop-final.png" alt="twist" /></div> <!-- ====================================End Site Logo and continuing on with AWESOME================= --> <!-- ============================Now, Let us animate some text and call it "Rotator Title"============ --> <div id="rotate"> <h1 class="rotate">RAYMOND<span> BROGAN</span><br><span>WEB | </span> AUDIO | <span>VIDEO </span></h1> <h1 class="rotate">We <span>Envision</span>. We <span>Design</span><br>We <span>Bring to Life</span>. Your Ideas</h1> </div> <!-- End Rotator Title --> <p>We are Coming</p> <p><span></span><i class="fa fa-heart"></i><span></span></p> <p>Really Soon</p> </div> <!-- Nav --> <nav> <ul class="navigation"> <li class="link left middle side move"><a href="#about" title="About">About</a></li> <li class="link right middle side move"><a href="#contact" title="Contact">Contact</a></li> <li class="link center bottom move"><a class="btn" href="#subscribe" title="Subscribe">Subscribe</a></li> </ul> </nav> <!-- End Nav --> </header> <!-- End Header --> <!-- About Section --> <section id="about" data-direction="from-left"> <div class="container"> <a href="#" class="close"></a> <h1> <span>About Us</span> </h1> <!-- ./End Section Title --> <div class="row text-center"> <div class="col-md-8 col-md-offset-2"> <h4>Lorem ipsum dolor sit amet consectetuer dis augue interdum quis a. Pretium habitant et volutpat egestas et elit facilisi et porttitor mauris.</h4> <p>Sed wisi congue egestas Vestibulum et augue mi et Vivamus Cum. Penatibus laoreet Nullam sagittis metus turpis non mus facilisi Sed justo. Libero felis tellus molestie Aliquam orci elit et pretium ac consectetuer. Vestibulum mus mollis Vestibulum augue sagittis convallis sit at nec a. Vitae Vestibulum Quisque facilisis sem Pellentesque dui quis augue amet sapien. Enim Nullam Sed tortor sem pellentesque cursus malesuada lacus eget sapien. Wisi porttitor Suspendisse.</p> </div> </div> </div> </section> <!-- End About Section --> <!-- Subscribe Section --> <section id="subscribe" data-direction="from-bottom"> <div class="content"> <h1> <span>Subscribe</span> </h1> <!-- ./End Section Title --> <a href="#" class="close"></a> <div class="container"> <div class="row text-center"> <div class="col-md-6 col-md-offset-3"> <!-- Subscribe form --> <div class="subscribe form-wrap"> <form action="#" method="post" id="subscribe-form"> <p class="form-field"> <input type="text" name="email" id="subscribe_email" placeholder="YOUR EMAIL ADDRESS"/> </p> <p class="form-submit"> <input type="submit" name="subscribe_submit" id="subscribe_submit" value="" /> </p> <span id="response"></span> </form> <p>We will notify you, when the site will launched. We don't do any spam with your email address.</p> </div> <!-- End Subscribe form--> <div class="social"> <a href="#" title="Twitter"><i class="fa fa-twitter"></i></a> <a href="#" title="Facebook"><i class="fa fa-facebook"></i></a> <a href="#" title="Pinterest"><i class="fa fa-pinterest"></i></a> <a href="#" title="Linkedin"><i class="fa fa-linkedin"></i></a> <a href="#" title="Dribble"><i class="fa fa-dribbble"></i></a> <a href="#" title="Flickr"><i class="fa fa-flickr"></i></a> <a href="#" title="Github"><i class="fa fa-github"></i></a> </div> </div> </div> </div> </div> </section> <!-- End Subscribe Section --> <!-- Contact Section --> <section id="contact" data-direction="from-right"> <div class="container"> <h1> <span>Contact</span> </h1> <!-- ./End Section Title --> <a href="#" class="close"></a> <div class="row"> <div class="col-md-8 col-md-offset-2"> <h4>Just want to say hello? Send me an email or fill out the form below and we will get back to you ASAP.</h4> <!-- Begin Phone --> <div class="newline col-sm-4 text-center"> <i class="fa fa-mobile fa-2x"></i> <br> +1 (123) 4567890 </div> <!-- End Phone --> <!-- Begin Email --> <div class="newline col-sm-4 text-center"> <i class="fa fa-envelope-o fa-2x"></i> <br> info@example.com </div> <!-- End Email --> <!-- Begin Address --> <div class="newline col-sm-4 text-center"> <i class="fa fa-home fa-2x"></i> <br> Twist Agency <br> Paltan Tower, Dhaka. </div> <!-- End Address --> <div class="clearfix"></div> <div class="contactForm"> <!-- Start Contact Form --> <form id="contactForm" action="#" method="post" role="form"> <div class="col-lg-6 col-xs-12"> <div class="form-group"> <input id="formName" type="text" class="input required" name="name" placeholder="Your name"> </div> <div class="form-group"> <input id="formEmail" type="email" class="input required" name="email" placeholder="Email address"> </div> <div class="form-group"> <input id="phone" type="text" placeholder="Phone" value="" size="30" name="phone"> </div> </div> <div class="col-lg-6 col-xs-12"> <div class="form-group"> <textarea id="message" class="textarea required" name="message" rows="3" cols="40" placeholder="Your question"></textarea> </div> <button value="Submit" id="submit" class="btn btn-green-border btn-lg" type="submit">Send</button> </div> </form> <div class="successMsg"></div> <!-- End Contact Form --> </div> </div> </div> </div> </section> <!-- End Contact Section --> <!-- Loading... --> <div id="preload"> <div id="preload-content"> <div class="preload-bounce"> <span class="bounce1"> </span> <span class="bounce2"> </span> <span class="bounce3"> </span> <span class="bounce4"> </span> </div> </div> </div> <!-- ============================================== JS =============================================== --> <script type="text/javascript" src="js/modernizr.custom.js"></script> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src ="js/bootstrap.min.js"></script> <script type="text/javascript" src="js/plugins.js"></script> <script type="text/javascript" src="js/jquery.velocity.min.js"></script> <script type="text/javascript" src="js/jquery.kenburnsy.min.js"></script> <script type="text/javascript" src="js/star.js"></script> <script type="text/javascript" src="js/init.js"></script> 只是一个数字(因此CGFloat17.3的值相同);您真正关心的是如何设置17.30号码的String表示。

作为@vadian解决方案的替代方案,您可以使用CGFloat,如下所示

NSNumberFormatter

答案 2 :(得分:0)

适用于swift 4

尝试以NumberFormatter格式使用.decimal

让text =&#34; 123456.789&#34;

let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
let value = formatter.string(from: text! as NSNumber)!

print(value)

Output: 123,456.78