Skip to content
Fix Code Error

java : convert float to String and String to float

March 13, 2021 by Code Error
Posted By: Anonymous

How could I convert from float to string or string to float?

In my case I need to make the assertion between 2 values string (value that I have got from table) and float value that I have calculated.

String valueFromTable = "25";
Float valueCalculated =25.0;

I tried from float to string:

String sSelectivityRate = String.valueOf(valueCalculated );

but the assertion fails

Solution

Using Java’s Float class.

float f = Float.parseFloat("25");
String s = Float.toString(25.0f);

To compare it’s always better to convert the string to float and compare as two floats. This is because for one float number there are multiple string representations, which are different when compared as strings (e.g. “25” != “25.0” != “25.00” etc.)

Answered By: Anonymous

Related Articles

  • How to aggregate values from a list and display in…
  • How do I include certain conditions in SQL Count
  • How can I resolve Web Component Testing error?
  • Examples of GoF Design Patterns in Java's core libraries
  • integrating disqus with emberjs only works on first…
  • Unable to run Robolectric and Espresso with a…
  • TLS 1.3 server socket with Java 11 and self-signed…
  • Sort table rows In Bootstrap
  • data.table vs dplyr: can one do something well the…
  • When I'm testing a web app by JUnit and Mockito I…
  • Eclipse will not start and I haven't changed anything
  • ClassNotFoundException:…
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • Neither BindingResult nor plain target object for…
  • Jetpack Compose and Hilt Conflict
  • I am trying to subtract but it is always adding one…
  • java IO Exception: Stream Closed
  • SQLException: No suitable Driver Found for…
  • SecurityException: Permission denied (missing…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Gradle error: Execution failed for task…
  • Using setDate in PreparedStatement
  • Adding up BigDecimals using Streams
  • How to convert java.util.Date to java.sql.Date?
  • Adding numbers and receiving a NaN
  • How to convert number to words in java
  • How to pass valueOf method which has several…
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Requested bean is currently in creation: Is there an…
  • Can't access Eclipse marketplace
  • How to prevent scrolling the whole page?
  • Using enums in a spring entity
  • Different between parseInt() and valueOf() in java?
  • SQL query return data from multiple tables
  • Reference - What does this regex mean?
  • android studio 0.4.2: Gradle project sync failed error
  • Programmatically Lighten or Darken a hex color (or…
  • Palindromic numbers in Java
  • Execution failed for task…
  • Js Calculation based on radio button, and some problems
  • org.gradle.api.tasks.TaskExecutionException:…
  • Using Auto Layout in UITableView for dynamic cell…
  • How do I print my Java object without getting…
  • Sorting A Table With Tabs & Javascript
  • What's the difference between Instant and LocalDateTime?
  • How to obtain the start time and end time of a day?
  • Design DFA accepting binary strings divisible by a…
  • Mock MQRFH2 header in JUnit Testing Error [MQRFH2…
  • C++ Need help sorting a 2D string array
  • How do I install Java on Mac OSX allowing version switching?
  • Error in inserting record due to 787 SQLite exception
  • Use of Jquery on scroll event
  • How to parse JSON file with Spring
  • Android- Error:Execution failed for task…
  • Configure hibernate to connect to database via JNDI…
  • Vaadin Spring Boot - There was an exception while…
  • How to filter a RecyclerView with a SearchView
  • Why cannot retrieve data from Firebase realtime…
  • Smart way to truncate long strings
  • Could not install Gradle distribution from…
  • What is the difference between `Enum.name()` and…
  • Calculator: Back key doesnt work in Javascript
  • Ukkonen's suffix tree algorithm in plain English
  • How to update other fields based on a field value in…
  • After a little scroll, the sticky navbar just is not…
  • Android Studio 4.2.1 NullPointer Exception on startup
  • Java string to date conversion
  • JPA Hibernate Persistence exception…
  • What is an optional value in Swift?
  • What are the undocumented features and limitations…
  • Active tab issue on page load HTML
  • How to Update Database from Assets Folder in App
  • ClassNotFoundException thrown
  • When should I use a table variable vs temporary…
  • Why does C++ code for testing the Collatz conjecture…
  • The definitive guide to form-based website authentication
  • Python: Float to Decimal conversion and subsequent…
  • Convert Java Date to UTC String
  • Algo not working for String Decode Ways -- ,…
  • What does "Could not find or load main class" mean?
  • String concatenation with Groovy
  • Best practice when asserting non-null on a value…
  • Java - Regex, nested recursion match
  • Spring Boot with ElasticSearch in Groovy: WebClient…
  • Pass values from Element UI autocomplete in the…
  • "Non-resolvable parent POM: Could not transfer…
  • Launching Spring application Address already in use
  • Getting Dropwizard Client And Jersey/HTTP I/O Error…
  • What does the CSS rule "clear: both" do?
  • Getting the closest string match
  • Eclipse fails to open .vue files in Web Page Editor
  • Error inflating class android.support.v7.widget.Toolbar?
  • java.lang.ClassNotFoundException: HttpServletRequest
  • Start redis-server with config file
  • How can I determine whether a 2D Point is within a Polygon?
  • Integer.valueOf() vs. Integer.parseInt()
  • UnsatisfiedDependencyException: Error creating bean…
  • How to generate a random string of a fixed length in Go?
  • How does PHP 'foreach' actually work?
  • What is the difference between…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I get the day of week given a date?

Next Post:

Return array in a function

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error