- Home /
The question is answered, right answer was accepted
Float to Integer
Hi all, i created a script for download my object from server. So i used calculating the download using following scripts.
using UnityEngine;
using System.Collections;
public class BundleLoad : MonoBehaviour
{
private string progress = " ";
private string logtxt;
public string url = " ";
GUIStyle Font_Size;
void Awake()
{
Font_Size = new GUIStyle ();
Font_Size.fontSize = 40;
Font_Size.normal.textColor = Color.black;
}
void log( string t )
{
logtxt += t + "/n";
}
void OnGUI()
{
Vector3 scale;
float ResolutionX = 1024;
float ResolutionY = 768;
scale.x = (float)Screen.width/ResolutionX;
scale.y = (float)Screen.height/ResolutionY;
scale.z = 1;
Matrix4x4 svMat = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(new Vector3(0,0,0),Quaternion.identity,scale);
GUI.Label (new Rect (560, 684, Screen.width - 5, Screen.height - 5), "Downloading: ", Font_Size);
GUI.Label(new Rect(795, 684, Screen.width - 5, Screen.height - 5), logtxt + " " + progress, Font_Size);
}
IEnumerator Start()
{
using(WWW www = WWW.LoadFromCacheOrDownload (url, 1))
{
while( !www.isDone )
{
progress = " " + www.progress * 100 + " %";
yield return null;
}
yield return www;
AssetBundle bundle = www.assetBundle;
bundle.LoadAll();
Application.LoadLevel("Bundle");
}
}
}
Here my downloading parts are working fine. But my download value display in decimal values, i want to show that in integer. i tried a way to convert that, but there is no use.
progress = " " + www.progress * 100 + " %";
You should clean your question to the only important part which is the conversion and display of the floating number www.progress to string without decimals.
The question of formating floats to string has been answered many times before:
Answer by Fonserbc · Dec 23, 2014 at 07:12 AM
There are several ways to get what you want, either casting or converting the float to int or use the built-in ToString() function of floats.
String withoutDecimals;
withoutDecimals = (www.progress * 100.0f).ToString("#");
// Or
withoutDecimals = ( (int)(www.progress * 100.0f).ToString();
// Or
withoutDecimals = ( Mathf.FloorToInt(www.progress * 100.0f) ).ToString();
// Or use either Mathf.CeilToInt or Mathf.RoundToInt depending on what you want
The # inside the ToString function, tells the function to just print the integer value of the float. Alternatively you can pass the function #.# to ask for a decimal, #.## for 2 decimals and so on.
if i use this: progress = " " + www.progress.ToString("#") * 100 + " %";
got an error.
or if i use this:
progress = " " + ((int)www.progress).ToString();
my values are not increasing.