- Home /
how do you get an input value from 0 to 1 and display a dollar value?
Hi there, I am trying to figure out how to display a text of a dollar value with a slider bar with a value from 0 (start) to 1 (end).
Here is a code that I created for a coin meter:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TimerScript : MonoBehaviour {
Image meterBarFill;
public float maxTime = 5f;
float timeLeft;
public GameObject collectCoinsGrayBtn;
public GameObject StoreItem_collectCoinsGreen;
// Use this for initialization
void Start ()
{
collectCoinsGrayBtn.SetActive (true);
StoreItem_collectCoinsGreen.SetActive (false);
meterBarFill = GetComponent<Image> ();
timeLeft = maxTime;
}
// Update is called once per frame
void Update ()
{
if (timeLeft > 0) {
timeLeft -= Time.deltaTime;
meterBarFill.fillAmount = timeLeft / maxTime;
} else {
collectCoinsGrayBtn.SetActive (false);
StoreItem_collectCoinsGreen.SetActive (true);
Time.timeScale = 1;
}
}
public void ResetMeter ()
{
meterBarFill = GetComponent<Image> ();
timeLeft = maxTime;
StoreItem_collectCoinsGreen.SetActive (false);
collectCoinsGrayBtn.SetActive (true);
if (timeLeft > 0) {
timeLeft -= Time.deltaTime;
meterBarFill.fillAmount = timeLeft / maxTime;
} else {
collectCoinsGrayBtn.SetActive (false);
StoreItem_collectCoinsGreen.SetActive (true);
Time.timeScale = 1;
}
}
}
Answer by tormentoarmagedoom · Jun 06, 2018 at 08:17 AM
Good day.
What you mean with a dollar value? a integrer number + dollar symbol?
You need to just create a text and change it to the value yu need. But.. from 0 to 1? from $0 to $1 ? or what?
You also have the properties of ToString() function to print it with the decimals you need, or you can transform a float into a integrer before print the value in the text....
Give more details of what you need...
As tormento says, the ToString function will probably do what you need for example:
float f = 0.546f;
Debug.Log(f.ToString("$0.00"));
Should print $0.55
You can also specify a locale as: f.ToString("$0.00", new System.Globalization.CultureInfo("en-GB"));
Your answer
